Using xpath and vtd-xml to get sub nodes and text of an element as a string

Deadly 提交于 2019-12-03 15:58:48

Below is the code that does what you are looking for.

    VTDGen vg =  new VTDGen();
    if (vg.parseFile("c://xml//alex.txt", true)){
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("//L/D");
        int i=-1;
        while((i=ap.evalXPath())!=-1){
            long l = vn.getContentFragment();
            System.out.println(" -==> "+ vn.toString((int )l, (int)(l>>32)));
        }
    }

Use:

/*/L/D/node()

This selects all nodes (elements, text-nodes, processing-instructions and comment-nodes) that are children of any D element that is a child of any L element that is a child of the top element of the XML document.

Alternatively, you could select separately all the node-children of the two /*/L/D elements:

/*/L[1]/D/node()

and

/*/L[2]/D/node()

Verification using XSLT as host of XPath:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/L[1]/D/node()"/>
--------------------
  <xsl:copy-of select="/*/L[2]/D/node()"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<MAIN>
    <L>
        <D>string1 string2 
            <b>string3</b> string4
        </D>
    </L>
    <L>
        <D>string5 string6 
            <b>string7</b> string8 
            <i>string9</i>
        </D>
    </L>
</MAIN>

the wanted, correct result is produced:

string1 string2 
            <b>string3</b> string4

--------------------
  string5 string6 
            <b>string7</b> string8 
            <i>string9</i>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!