XMLOutputStream, repairing namespaces, and attributes without namespaces
- by comment_bot
A simple task: write an element with an unnamespaced attribute:
String nsURI = "http://example.com/";
XMLOutputFactory outF = XMLOutputFactory.newFactory();
outF.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
XMLStreamWriter out = outF.createXMLStreamWriter(System.out);
out.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "element", nsURI);
out.writeAttribute(XMLConstants.DEFAULT_NS_PREFIX,
XMLConstants.NULL_NS_URI, "attribute", "value");
out.writeEndElement();
out.close();
Woodstox's answer:
<element xmlns="http://example.com/" attribute="value"></element>
JDK 6 answer:
<zdef-159241566:element xmlns="" xmlns:zdef-159241566="http://example.com/" attribute="value"></zdef-159241566:element>
What?!
Further, if we add a prefix to the element:
out.writeStartElement(ns, "element", nsURI);
JDK 6 no longer attempts to emit xmlns="":
<ns:element xmlns:ns="http://example.com/" attribute="value"></ns:element>
I'm fairly sure this is a bug in JDK 6. Am I right? And could anyone suggest a work around that will keep both libraries (and any others) happy? I don't want to require woodstox if I can help it.