I have a fairly large repetitive XML to create using JAXB. Storing the whole object in the memory then do the marshaling takes too much memory. Essentially, my XML looks like this:
<Store>
<item />
<item />
<item />
.....
</Store>
Currently my solution to the problem is to "hard code" the root tag to an output stream, and marshal each of the repetitive element one by one:
aOutputStream.write("<?xml version="1.0"?>")
aOutputStream.write("<Store>")
foreach items as item
aMarshaller.marshall(item, aOutputStream)
end
aOutputStream.write("</Store>")
aOutputStream.close()
Somehow the JAXB generate the XML like this
<Store xmlns="http://stackoverflow.com">
<item xmlns="http://stackoverflow.com"/>
<item xmlns="http://stackoverflow.com"/>
<item xmlns="http://stackoverflow.com"/>
.....
</Store>
Although this is a valid XML, but it just looks ugly, so I'm wondering is there any way to tell the marshaller not to put namespace for the item elements? Or is there better way to use JAXB to serialize to XML chunk by chunk?
If you don't specifiy a namespace JaxB will not write one.
Yout could use Stax on a Stream, if your strcuture is not to complicated.
The following did the trick for me:
This works for me:
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "");
For me, simply calling
xmlStreamWriter.setDefaultNamespace("")
solved the issue.One more thing you have to care in order to remove the namespace prefix from the output is that everywhere you have @XmlElement ensure it does not include the namespace property like
@XmlElement(name="", namespace"http://...")
; otherwise, none of solutions will work.I've tried all solutions provided as answers here and none of them worked for my environment. My current requirements are:
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:
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
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.
Check your
package-info.java
(in the package where your jaxb-annotated classes are). There is thenamespace
attribute of@XmlSchema
there.Also, there is a
namespace
attribute in the@XmlRootElement
annotation.