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--