tags:

views:

329

answers:

2

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"&gt;
  <item xmlns="http://stackoverflow.com"/&gt;
  <item xmlns="http://stackoverflow.com"/&gt;
  <item xmlns="http://stackoverflow.com"/&gt;
.....
</Store>

Although this is a valid XML, but it just look ugly, so I'm wonder 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?

A: 

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.

tkr
but the fact is I need a namespace, but just at the root element level.
Alvin
+2  A: 

Check your package-info.java (in the package where your jaxb-annotated classes are). There is the namespace attribute of @XmlSchema there.

Also, there is a namespace attribute in the @XmlRootElement annotation.

Bozho
Is there a way to specify in the binding file not to put namespace in my package-info.java and only put the namespace in @XmlRootElement?
Alvin
you can override it in `@XmlRootElement`, but as far as I understand, you want it removed completely.
Bozho