Reputation: 621
I use JDOM2 to retrieve XML I don't control from remote feeds. For one of them I got this error:
The name "" is not legal for JDOM/XML namespaces: Namespace URIs must be non-null and non-empty Strings.
Here is a sample of the XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Result xmlns="urn:XYZ" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeA="1" attributeB="2" attributeC="3" xsi:schemaLocation="http://XYZ.com/ZYZ.xsd">
...
</Result>
If I remove xsi:schemaLocation with a Regex, that error goes away.
private String stripSchemaLocation(String xml) {
return xml.replaceAll("xsi:schemaLocation=\"(.+?)\"", "");
}
Here is my JDOM2 parsing code. It fails on builder.build
// Schema location is causing problem with some xml.
xml = stripSchemaLocation(xml);
SAXBuilder builder = new SAXBuilder();
//@see http://xerces.apache.org/xerces-j/features.html
builder.setFeature("http://xml.org/sax/features/validation", false);
builder.setFeature("http://xml.org/sax/features/namespaces", false);
builder.setFeature("http://apache.org/xml/features/validation/schema", false);
//@see http://www.jdom.org/docs/faq.html#a0350
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
org.jdom2.Document doc2 = builder.build(new InputSource(new StringReader(xml)));
// --Then doing XPath stuff with this XML--
Upvotes: 1
Views: 5945
Reputation: 17707
From the JavaDoc: http://www.jdom.org/docs/apidocs/org/jdom2/input/SAXBuilder.html#setFeature(java.lang.String,%20boolean)
Also, this should never be done when using JDOM:
builder.setFeature("http://xml.org/sax/features/namespaces", false);
See the package documentation too http://jdom.org/docs/apidocs/org/jdom2/input/sax/package-summary.html , where it states:
Note that the existing JDOM implementations described above all set the generated XMLReaders to be namespace-aware and to supply namespace-prefixes. Custom implementations should also ensure that this is set unless you absolutely know what you are doing
What you probably want to do is simply:
SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
which, as it happens, is the same as:
SAXBuilder builder = new SAXBuilder();
And thus your code would be:
SAXBuilder builder = new SAXBuilder();
//@see http://www.jdom.org/docs/faq.html#a0350
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
org.jdom2.Document doc2 = builder.build(new InputSource(new StringReader(xml)));
Upvotes: 1