JDOM 2.x vs 1.x compatibility problem when parsing DOM documents #138

Closed
rolfl opened this Issue Nov 5, 2014 · 0 comments

Projects

None yet

1 participant

@rolfl
Collaborator
rolfl commented Nov 5, 2014

JDOM 2.x handles namespace aware documents well, but documents created manually, in a non-aware way, and then parsed by DOM builder, do not produce the 'right' results in JDOM 2.x even though JDOM 1.x produces the correct output.

See Stack Overflow - How to prevent XMLOutputter in JDOM2 from cutting attribute name (namespace-part)?

This is not to say that JDOM 2.x is doing the wrong thing, just that JDOM 1.x handles poorly-created DOM input better than JDOM 2.x.

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    org.w3c.dom.Document doc = dbFactory.newDocumentBuilder().newDocument();
    doc.setXmlVersion("1.0");

    Element root = doc.createElement("Document");

    root.setAttribute("xmlns", "urn:iso:foo");
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("xsi:schemaLocation", "urn:iso:foo bar.xsd");
    doc.appendChild(root);

The above code, when processed by JDOM 1.x DOMBuilder, produces:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:foo bar.xsd" />

but when processed by JDOM 2.x it produces:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="urn:iso:foo bar.xsd" />

(note the missing xsi: prefix).

If the DOM document is built with:

    root.setAttribute("xmlns", "urn:iso:foo");
    root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", "urn:iso:foo bar.xsd");
    doc.appendChild(root);

then it works as expected

@rolfl rolfl added the Target 2.0.x label Feb 13, 2015
@rolfl rolfl added a commit that referenced this issue Feb 15, 2015
@rolfl rolfl Fixes #138 - backward ccompatibility issue with JDOM 1.x DOM
JDOM 1.x managed to handle badly-built DOM documents that had implied
namespaces in place.
2e946f8
@rolfl rolfl added a commit that closed this issue Feb 15, 2015
@rolfl rolfl Fixes #138 - backward ccompatibility issue with JDOM 1.x DOM
JDOM 1.x managed to handle badly-built DOM documents that had implied
namespaces in place.
ae05d54
@rolfl rolfl closed this in ae05d54 Feb 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment