我正试图从XML节点中获取值并运行CDATA部分的问题。
我的XML看起来像:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<Test>This is my node</Test>
<HelpContent><![CDATA[this is the CDATA section]]></HelpContent>
</root>
使用此代码:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(currentFile);
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpathObj = xPathFactory.newXPath();
XPathExpression expr = xpathObj.compile("//*");
NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
int len = nodes.getLength();
for (int i = 0; i < len; i++){
Node node = nodes.item(i);
System.out.println("Node name [" + node.getNodeName() + "] of type [" + node.getNodeType() + "]");
System.out.println("NodeValue: " + node.getNodeValue());
System.out.println("TextContent: " + node.getTextContent());
}
我得到了以下内容:
> Node name [root] of type [1]
> NodeValue: null
> TextContent: This is my
> node this is the CDATA section
>
> Node name [Test] of type [1]
> NodeValue: null
> TextContent: This is my node
>
> Node name [HelpContent] of type [1]
> NodeValue: null
> TextContent: this is the CDATA section
如您所见,对于具有子节点的节点(在这种情况下只有根节点),我获得了从子节点提取的所有文本。 此外,您可以看到getNodeType始终重新调整1(ELEMENT_NODE)...
如果包含像“Test”和“TextContent”这样的数据但是对于像“root”这样的节点为空或null,我怎么才能得到node的值?
谢谢。
答案 0 :(得分:0)
我提出了这个解决方案......不确定这是否是正确的方法,但似乎按预期工作。
因此,要获取“Test”或“HelpContent”等节点的值,我使用以下代码更新了代码:
NodeList childs = node.getChildNodes();
if (childs.getLength() == 1){
System.out.println("TextContent: " + node.getTextContent());
}