I’m having some trouble successfully marshalling using the Marshaller.JAXB_FRAGMENT property. Here’s a simple version of the XML i’m trying to output.
<Import>
<WorkSets>
<WorkSet>
<Work>
<Work>
...
..
...
</WorkSet>
<WorkSet>
<Work>
<Work>
...
</WorkSet>
<WorkSets>
<Import>
The <Import> and <WorkSets> elements are essentially just container elements that enclose a large number of <WorkSet> & <Work> elements. I’m currently trying to marshall at the <WorkSet>.
- Is it possible to initially marshal the
<Import>and<WorkSets>elements and then from then on marshal at the<WorkSet>element and have the output be enclosed in the<Import><WorkSets>tags? - When I’m marshaling at the WorkSet level it attaches the
xmlns='http://namespace.com'attribute to the WorkSet tag, is there a way to marshal without the namespace attribute being attached to Workset?
Basically, it sounds like rather than constructing a full object tree with the container objects, you want to be able to stream a collection of WorkSet instances to marshal using JAXB.
The approach I would take is to use an XMLStreamWriter and marshal the WorkSet objects by wrapping them in a JAXBElement. I don’t have tested sample code close at hand, so here’s the rough code snippet that should put you on the write track:
Note: The above is completely untested and may be messing something up in the wrapping part to write each instance of WorkSet. You need to wrap the WorkSet instances because they will not be annotated with
@XmlRootElementand JAXB will otherwise refuse to marshal the objects.