I am trying to understand the very great tutorial on XPath in Java (http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/). The explanation for getting strings of text from elements is superb. But I cannot find a way to get text and inline elements. For the XML in the tutorial I added a <b> inline element:
<Employee emplid="3333" type="user">
<firstname>Big <b>Jim</b></firstname>
<lastname>Moriarty</lastname>
<age>52</age>
<email>jim@sh.com</email>
</Employee>
I use the code from the tutorial:
System.out.println("**2***********************");
expression = "/Employees/Employee/firstname";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
And it skips the inline:
/Employees/Employee/firstname
Big
I wish to get the whole contents of <firstname>, including the tag, like this
/Employees/Employee/firstname
Big <b>Jim</b>
I am happy to supply more code if needed.