1

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.

  • And I recommend [this answer in particular](http://stackoverflow.com/a/5948326/592139). – Ian Roberts Jun 28 '14 at 01:02
  • I tried your recommended answer. It does not work for me (I must be missing an obvious step). I added public String innerXml(Node node2) . . . And called it: innerXml(node2); . . . System.out.println(node2.getFirstChild().getTextContent()); - But did not get the inline elements. I don't mean to ask anybody to write my code, but perhaps an example of making your preferred answer work with the code I've supplied in my sample? Thanks! – Jedi Grunge Jun 28 '14 at 18:05
  • `System.out.println(innerXml(node2))` should produce the result you want. If it didn't then edit the question to indicate exactly what you have tried and what the result was and we can reopen the question and help you debug. – Ian Roberts Jun 29 '14 at 18:21
  • Your `node2` has two child nodes, the first child is the four character text node B-i-g-space and the second child is the `b` element. That's why `getFirstChild` will only ever return you the "Big ". – Ian Roberts Jun 29 '14 at 18:24
  • @Ian your clarification worked beautifully. And with the help of the OP's additional note of lsSerializer.getDomConfig().setParameter("xml-declaration", false); - I now have my code working perfectly. I am new to stackoverflow, but I wish I could mark your answer as accepted. But I guess I cannot do that on a comment. My heartfelt gratitude to you sir! – Jedi Grunge Jun 29 '14 at 20:30

1 Answers1

0

Try nodeList.item(i).getFirstChild().getTextContent()

It will return the node and all child nodes as a string.

  • Thanks. I tried, but it did not work. Changed to System.out.println(nodeList.item(i).getFirstChild().getTextContent()); - but I got the some result as before. Maybe I need to do more than this? – Jedi Grunge Jun 28 '14 at 14:56