当我尝试解析XML文件时,我得到一个空节点.
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = null;
try {
node = (Node) xPath.evaluate(
"/mynode",
doc,
XPathConstants.NODE);
Run Code Online (Sandbox Code Playgroud)
我仅在以下情况下面临此问题 -
1. DocumentBuilderFactory-setNameSpaceAware为true
2. DocumentBuilderFactory- setValidating 为true.
如果这些设置为false,那么我得到的结果是正确的.任何人都可以帮助我理解将这些属性设置为false的关系是什么?(我已经检查了这个问题,但这并不能说明我的疑问)
这是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>
Run Code Online (Sandbox Code Playgroud)