I a started to use xml.jcabi xml library. It looks like a simple library but I am not able to query nodes if xmlns namespace is set.
Here is my xml file:
<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>
And my Java code looks like this:
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);
}
My problem is that xmlAdHocRequest.nodes("//xmlns:RequestData")
returns zero nodes. If I remove the default namespace from the XML file and (obviously) from NS register than the query works.
I also tried to register the xmlns (default) namespace by setting its prefix to m istead of 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);
}
When i try to fetch nodes I only got an error:
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)
My question is: am I doing something wrong or is this some kind of xml.jcabi issue?