Skip to content

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

@rolfl

Description

@rolfl

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions