當我試圖解析XML文件時,我得到一個空節點。當isNameSpaceAware和isValidating爲「true」時,XPath爲「節點」返回null
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>
是的。我的XML文件使用一個名稱空間。但我沒有得到你的第二點。對不起,但我是XPath相關內容的新手。你能否詳細說明如何做到這一點? - 「你的XPath查詢似乎只是查詢emtpy命名空間,因此你不會得到一個結果,確保使用正確的命名空間,它將工作」 – Pramod