我开始使用xml.jcabi xml 库。它看起来像一个简单的库,但如果设置了 xmlns 命名空间,我将无法查询节点。
这是我的xml文件:
<MyRequestData xmlns='http://www.myaddress.com/myfile.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<RequestData>
<startDate>2016-03-25</startDate>
<endDate>2016-03-25</endDate>
</RequestData>
</MyRequestData>
我的 Java 代码如下所示:
XML xmlRequest = new XMLDocument(requestFileIS);
xmlRequest.registerNs("xmlns", "http://www.myaddress.com/myfile.xsd");
for (XML requestData : xmlAdHocRequest.nodes("//xmlns:RequestData"))
{
String startDate = requestData.xpath("startDate/text()").get(0);
}
我的问题是xmlAdHocRequest.nodes("//xmlns:RequestData")
返回零节点。如果我从 XML 文件和(显然)从 NS 寄存器中删除默认命名空间,则查询将起作用。
我还尝试通过将其前缀设置为 mi 而不是 xmlns 来注册 xmlns(默认)命名空间:
XML xmlAdHocRequest = new XMLDocument(requestFileIS);
xmlRequest.registerNs("m", "http://www.myaddress.com/myfile.xsd");
for (XML requestData : xmlRequest.nodes("//m:RequestData")) {
String startDate = requestData.xpath("startDate/text()").get(0);
}
当我尝试获取节点时,我只收到一个错误:
java.lang.IllegalArgumentException: invalid XPath query '//x:RequestData' by org.apache.xpath.jaxp.XPathFactoryImpl
at org.apache.xpath.compiler.XPathParser.errorForDOM3(XPathParser.java:655)
at org.apache.xpath.compiler.Lexer.mapNSTokens(Lexer.java:647)
at org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:365)
at org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:98)
at org.apache.xpath.compiler.XPathParser.initXPath(XPathParser.java:112)
at org.apache.xpath.XPath.<init>(XPath.java:178)
at org.apache.xpath.XPath.<init>(XPath.java:266)
at org.apache.xpath.jaxp.XPathImpl.eval(XPathImpl.java:195)
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:281)
at com.jcabi.xml.XMLDocument.fetch(XMLDocument.java:429)
at com.jcabi.xml.XMLDocument.nodes(XMLDocument.java:352)
我的问题是:我做错了什么还是这是某种xml.jcabi问题?