I get a XML file from website (http://www.abc.com/),
URL is: http://www.abc.com/api/api.xml
content is:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.abc.com/">
<name>Hello!</name>
</root>
it has xmlns="http://www.abc.com/" in XML file,
now, I using JDOM XPath to get text Hello!
XPath xpath = XPath.newInstance("/root/name/text()");
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new URL("http://www.abc.com/api/api.xml"));
System.out.println(xpath.valueOf(doc)); //nothing to print...
I test to remove xmlns="http://www.abc.com/" from XML file, it’s be work!
how to change my java code to get Hello!, if xmlns="http://www.abc.com/" exist?
(I can’t chagne this XML file)
thanks for help 🙂
You’ll need to make the query aware of the xml namespace. This answer here looks like it will do the trick:
Default Xml Namespace JDOM and XPATH
You might also change your query to use local-name to ignore namespaces:
That should return the node named root. That is, if it supports it and I typed it correctly! 🙂 I’m not familiar with java API’s for XML + XPATH.
Be aware that xml namespaces exist to distinguish node ‘root’ from any other node named ‘root’. Just like class/package namespaces. Ignoring them could lead to a name collision. Your milage may vary.
HTH,
Zach