Using xpath and vtd-xml to get auxiliary nodes and element text as a string

This is part of my XML:

<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> I want to get the content of all the <D> tags as string. So, the example above should return: 1st iteration: 'string1 string2 <b>string3</b> string4' 2nd iteration: 'string5 string6 <b>string7</b> string8 <i>string9</i>' etc... 

In vtd-xml, I used AutoPilot with XPath "// L / D" and "// L / D / text ()", but that didn't work.

Any advice or alternative approach will be appreciated.

Hi

+6
source share
2 answers

Below is the code that executes 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))); } } 
+12
source

Using

 /*/L/D/node() 

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

Alternatively, you can select separately all node -children from two /*/L/D elements :

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

and

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

Validation using XSLT as the XPath host :

 <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 to 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> 

required, the correct result is obtained :

 string1 string2 <b>string3</b> string4 -------------------- string5 string6 <b>string7</b> string8 <i>string9</i> 
+3
source

Source: https://habr.com/ru/post/888127/


All Articles