remove xmlns attribute from the root element while marshalling jaxb

我怕爱的太早我们不能终老 提交于 2019-12-03 16:07:36
sergio

I have similar requirements at the moment. The only solution that worked for me is implementing a wrapper for XMLStreamWriter.

Please, take a look at my answer here. I've also described there other solutions I tried out.

Serialization using code from the link above looks like this:

XMLOutputFactory factory = XMLOutputFactory.newFactory();

StringWriter writer = new StringWriter(XML_BUFFER_INITIAL_SIZE);
XMLStreamWriter xmlWriter = null;

try {
  xmlWriter = factory.createXMLStreamWriter(writer);
  JAXBContext context = JAXBContext.newInstance(MyJAXBGeneratedClass.class);
  Marshaller marshaller = context.createMarshaller();
  marshaller.marshal(reportContainer, new NamespaceStrippingXMLStreamWriter(xmlWriter));
  xmlWriter.flush();
}
finally {
  if (xmlWriter != null)
    xmlWriter.close();
}

return writer.toString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!