The default implementation of DOMOutputProcessor generates xmlns attributes without setting the namespace. On the JAXP in Oracle Java 1.7 this means the generated Attrs have a null Namespace URI. This is causing us problems when serialising these Elements as part of a JAXB object (using EclipseLink MOXy - the Metro implementation is able to cope with it).
I'm not entirely sure if this is strictly a bug in JDOM or if the fault lies with JAXP for not inferring the Namespace URI on an element prefixed with "xmlns" - but it does mean the Documents coming from DOMOutputter don't look the same as a document coming from the built-in DocumentBuilderFactory.
Here's a JUnit test to reproduce the issue.
importorg.jdom2.Element;
importorg.jdom2.JDOMConstants;
importorg.jdom2.input.DOMBuilder;
importorg.jdom2.input.SAXBuilder;
importorg.jdom2.output.DOMOutputter;
importorg.junit.Test;
importorg.w3c.dom.Document;
importorg.xml.sax.InputSource;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjava.io.StringReader;
importstaticorg.junit.Assert.assertEquals;
publicclassDOMOutputterNamespaceTest
{
/** * The XML document to test - this document */privatestaticfinalStringXML = "<el xmlns:test=\"urn:test\" />";
/** * Parse the XML using DOM * * @throws Exception */@TestpublicvoidtestDocumentBuilderSource() throwsException
{
org.w3c.dom.Elementelement = parseDOM(XML);
finalStringnamespace = element.getAttributeNode("xmlns:test").getNamespaceURI();
assertEquals("DocumentBuilder output", JDOMConstants.NS_URI_XMLNS, namespace);
}
/** * Parse the XML using JDOM, convert to DOM * * @throws Exception */@TestpublicvoidtestJdomToDom() throwsException
{
org.w3c.dom.Elementelement = jdomToDom(parseJDOM(XML)); // load JDOM and convert to DOMfinalStringnamespace = element.getAttributeNode("xmlns:test").getNamespaceURI();
assertEquals("SAXBuilder->DOMOutputter output", JDOMConstants.NS_URI_XMLNS, namespace);
}
/** * Parse the XML using DOM, then convert to JDOM and then finally back to DOM * * @throws Exception */@TestpublicvoidtestDomToJdomToDom() throwsException
{
org.w3c.dom.Elementelement = jdomToDom(domToJdom(parseDOM(XML))); // load DOM, convert to JDOM and back to DOMfinalStringnamespace = element.getAttributeNode("xmlns:test").getNamespaceURI();
assertEquals("DocumentBuilder->DOMBuilder->DOMOutputter output", JDOMConstants.NS_URI_XMLNS, namespace);
}
privateElementparseJDOM(Stringxml) throwsException
{
finalStringReadersrc = newStringReader(xml);
returnnewSAXBuilder().build(src).getRootElement();
}
privateorg.w3c.dom.ElementparseDOM(Stringxml) throwsException
{
InputSourcesrc = newInputSource(newStringReader(xml));
DocumentBuilderFactoryfactory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilderdocumentBuilder = factory.newDocumentBuilder();
Documentresult = documentBuilder.parse(src);
returnresult.getDocumentElement();
}
/** * Converts a JDOM Element to a DOM Element * * @param element * * @return * * @throws Exception */privateorg.w3c.dom.ElementjdomToDom(org.jdom2.Elementelement) throwsException
{
returnnewDOMOutputter().output(element);
}
/** * Converts a DOM Element to a JDOM Element * * @param element * * @return * * @throws Exception */privateorg.jdom2.ElementdomToJdom(org.w3c.dom.Elementelement) throwsException
{
finalDOMBuilderbuilder = newDOMBuilder();
returnbuilder.build(element);
}
}
On our machines (Oracle Java 1.7, JDOM 2.0.4) the output is as follows:
testDocumentBuilderSource: pass
testJdomToDom: fail
java.lang.AssertionError: SAXBuilder->DOMOutputter output expected:<http://www.w3.org/2000/xmlns/> but was:<null>
testDomToJdomToDom: fail
java.lang.AssertionError: DocumentBuilder->DOMBuilder->DOMOutputter output expected:<http://www.w3.org/2000/xmlns/> but was:<null>
The text was updated successfully, but these errors were encountered:
I've got a patch to AbstractDOMOutputProcessor that should work at petergeneric@c27a4fc and a unit test as part of petergeneric@2c32cc5 (it doesn't use the same structure as the other jdom unit tests though - added it in a separate commit for clarity)
Hello,
The default implementation of DOMOutputProcessor generates xmlns attributes without setting the namespace. On the JAXP in Oracle Java 1.7 this means the generated Attrs have a null Namespace URI. This is causing us problems when serialising these Elements as part of a JAXB object (using EclipseLink MOXy - the Metro implementation is able to cope with it).
I'm not entirely sure if this is strictly a bug in JDOM or if the fault lies with JAXP for not inferring the Namespace URI on an element prefixed with "xmlns" - but it does mean the Documents coming from DOMOutputter don't look the same as a document coming from the built-in DocumentBuilderFactory.
Here's a JUnit test to reproduce the issue.
On our machines (Oracle Java 1.7, JDOM 2.0.4) the output is as follows:
The text was updated successfully, but these errors were encountered: