Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMDocument getNodeValue() returns null (contains an output escaped string)

I am processing a DomDocument which is basically the XML result of a SOAP web service. To give you an idea, this is what it looks like

...<ParentNode><ChildNode>&lt;output&gt;&lt;escaped&lt;string</ChildNode></ParentNode>...

Yes, the value of ChildNode is a string that has been output escaped and is XML that is packed within this XML. I do the usual run of DomDocument processing such as

NodeList rows = dom.getElementsByTagName(ChildNode);
for(int i=0;i<rows.length;i++)
{
  System.out.println(rows[i].getParentNode()); // returns ParentNode
  System.out.println(rows[i].getNodeName()); // returns ChildNode
  System.out.println(rows[i].getNodeValue()); // returns null
}

After you inspect the above code, you realize that even though the node returns correct values for ParentNode and the NodeName node, it returns a null value upon accessing getNodeValue(). There is a string here, and I can see it in my console output. But I am not sure what trick I am missing here, does the output escaping mess it up in any particular way?

Thanks, Parijat

like image 689
user1020069 Avatar asked Sep 13 '12 19:09

user1020069


2 Answers

You want getTextContent() rather than getNodeValue() - the latter always returns null for element nodes.

like image 159
Ian Roberts Avatar answered Sep 20 '22 21:09

Ian Roberts


Instead of

rows[i].getNodeValue()

use

rows[i].getNodeValue().getChildNodes().item(0).getNodeValue()
like image 1
Mike-Bell Avatar answered Sep 22 '22 21:09

Mike-Bell