I've tried all solutions provided as answers here and none of them worked for my environment. My current requirements are:
- jdk1.6.0_45
- JAXB annotated classes are generated upon every rebuild, so
changing them is not an option.
I'd like to share with you the results my experiments with solutions I've found on stackoverflow.
Custom NamespaceContext link
Simple and elegant solution, but in my environment I have an exception with the following stacktrace:
javax.xml.stream.XMLStreamException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1473)
at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1502)
at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1663)
at com.ctc.wstx.sw.BaseStreamWriter.close(BaseStreamWriter.java:288)
at MyDataConverter.marshal(MyDataConverter.java:53)
I got stuck trying to figure out why this exception occurs and decided to try out something else.
Modification of package-info.java link
The simplest solution I've found. It generally works, but this file is generated upon every build. That's why I have to find another solution.
Schema modification link
Works as described but doesn't solve my problem. I still have a namespace in root element.
DelegatingXMLStreamWriter link
I've also tried solutions mentioned there, but I had a strange assertion in com.sun.xml.bind.v2.runtime.output.NamespaceContextImpl (method declareNsUri) which I've failed to defeat.
My solution
While researching the issue with assertion I had to implement my own version of XMLStreamWriter based on DelegatingXMLStreamWriter.java
public class NamespaceStrippingXMLStreamWriter extends DelegatingXMLStreamWriter {
public NamespaceStrippingXMLStreamWriter(XMLStreamWriter xmlWriter) throws XMLStreamException {
super(xmlWriter);
}
@Override
public void writeNamespace(String prefix, String uri) throws XMLStreamException {
// intentionally doing nothing
}
@Override
public void writeDefaultNamespace(String uri) throws XMLStreamException {
// intentionally doing nothing
}
@Override
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
super.writeStartElement(null, local, null);
}
@Override
public void writeStartElement(String uri, String local) throws XMLStreamException {
super.writeStartElement(null, local);
}
@Override
public void writeEmptyElement(String uri, String local) throws XMLStreamException {
super.writeEmptyElement(null, local);
}
@Override
public void writeEmptyElement(String prefix, String local, String uri) throws XMLStreamException {
super.writeEmptyElement(null, local, null);
}
@Override
public void writeAttribute(String prefix, String uri, String local, String value) throws XMLStreamException {
super.writeAttribute(null, null, local, value);
}
@Override
public void writeAttribute(String uri, String local, String value) throws XMLStreamException {
super.writeAttribute(null, local, value);
}
}
The main idea is to avoid passing namespace information to the underlying XMLStreamWriter. Hope this will help you to save some time solving similar problem.
PS. It's not necessary to extend DelegatingXMLStreamWriter in your code. I've done this to show which methods need changing.