XMLOutputStream, repairing namespaces, and attributes without namespaces
Posted
by comment_bot
on Stack Overflow
See other posts from Stack Overflow
or by comment_bot
Published on 2010-06-18T10:40:55Z
Indexed on
2010/06/18
10:43 UTC
Read the original article
Hit count: 655
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.
© Stack Overflow or respective owner