This is a portion 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 an AutoPilot with XPath "//L/D" and "//L/D/text()" but that did not work.
Any advice or alternative approach will be appreciated.
Regards
Definition of XPath text XPath text () function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes.
Java example find xml element with attribute value using xpath Let’s look at the code which has been used to evaluate above xpath expressions to select nodes having certain attribute value. 2.1. XPath evaluate example Read XML file into org.w3c.dom.Document. Create XPathFactory with its newInstance () static method.
You can use XPath to find a single, specific node or to find all nodes that match some criteria. The DOM classes provide two methods for XPath selection: the SelectSingleNode method and the SelectNodes method.
The text of the web element is used by the function to locate the element on the webpage. This function comes in handy if your element contains text, such as labels, which always have static text. The text () function in XPath has the following syntax:
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)));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With