DOMDocument getNodeValue()返回null(包含输出转义字符串) [英] DOMDocument getNodeValue() returns null (contains an output escaped string)
问题描述
我正在处理基本上是SOAP Web服务的XML结果的DomDocument。
给你一个想法,这就是它的样子
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><output><escaped<string</ChildNode></ParentNode>...
是的,ChildNode的值是一个已被转义的字符串,是XML内的XML。我通常执行DomDocument处理,如
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
}
检查上述代码后,您会发现即使该节点返回ParentNode和NodeName节点的正确值,它在访问getNodeValue()时返回一个空值。这里有一个字符串,我可以在我的控制台输出中看到它。但是我不知道我在这里遇到什么技巧,输出是否以任何特定的方式解决了问题?
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?
谢谢,
Parijat
Thanks, Parijat
推荐答案
您需要 getTextContent()
而不是 getNodeValue()
- 后者总是为元素节点返回null。
You want getTextContent()
rather than getNodeValue()
- the latter always returns null for element nodes.
这篇关于DOMDocument getNodeValue()返回null(包含输出转义字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!