【发布时间】:2011-07-20 10:18:11
【问题描述】:
当我尝试解析 XML 文件时,我得到一个空节点。
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = null;
try {
node = (Node) xPath.evaluate(
"/mynode",
doc,
XPathConstants.NODE);
我只在以防万一的情况下遇到这个问题-
1. DocumentBuilderFactory-setNameSpaceAware 为真
2. DocumentBuilderFactory-setValidating 为真。
如果这些设置为 false,那么我会得到正确的结果。谁能帮助我理解将这些属性设置为 false 的关系是什么? (我已经检查了this的问题,但它并没有清除我的疑问)
这里是xml-
<?xml version="1.0" encoding="UTF-8"?>
<mynode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.com" xsi:schemaLocation="http://www.example.com example.xsd">
<name>TargetName</name>
<desc>desc goes here</desc>
<pack>my.this</pack>
<object>my.ExampleObject</object>
<properties>
<attrib>
<name>id</name>
<value>ZZZ</value>
</attrib>
<attrib>
<name>ind</name>
<value>X</value>
</attrib>
</properties>
<children>
<child>
<name>childnodename</name>
<desc>description goes here</desc>
<invalues>
<scope>ALL</scope>
</invalues>
<outvalues>
<scope>ALL</scope>
</outvalues>
<akey>
<aname>AAA</aname>
<key></key>
</akey>
<msg>
<success>code1</success>
<failure>code2</failure>
</msg>
</child>
</children>
</mynode>
【问题讨论】: