8

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>.

  1. 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?
  2. 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?
Perception
  • 79,279
  • 19
  • 185
  • 195
TyC
  • 792
  • 6
  • 11
  • 23
  • I am not sure I understand you. What are the "and elements"? I see only WorkSet, WorkSets and Import. What do you mean when you say "... initially marshall the and elements and then from then on marshall at the element ..." ? – AlexR Feb 15 '12 at 14:46
  • @AlexR When i first posted the question, it wasnt displaying the tags in the questions, I edited it so they should now be showing, let me know if they're not. – TyC Feb 15 '12 at 14:48
  • Why you have duplicated the question as [JAXB Fragment Marshal w/o namespace](http://stackoverflow.com/questions/9297872) if you have accepted the answer in this post? At least mention that the question was forked... – dma_k Feb 19 '12 at 00:34

1 Answers1

18

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:

FileOutputStream fos = new FileOutputStream("foo.xml");
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(fos);

writer.writeStartDocument();
writer.writeStartElement("Import");
writer.writeStartElement("WorkSets");

JAXBContext context = JAXBContext.newInstance(WorkSet.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 
for (WorkSet instance : instances)
{
    JAXBElement<WorkSet> element = new JAXBElement<WorkSet>(QName.valueOf("WorkSet"), WorkSet.class, instance);
    m.marshal(element, writer);
}

writer.writeEndDocument(); // this will close any open tags
writer.close();

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 @XmlRootElement and JAXB will otherwise refuse to marshal the objects.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
Ophidian
  • 9,775
  • 2
  • 29
  • 27
  • Thanks @JörnHorstmann, I'd forgotten about the fragment part and was just coming back to add it in myself. – Ophidian Feb 15 '12 at 16:04
  • 1
    Thanks! I just gave this a try and it seems to working well. I added a second question, when the WorkSet instances are being marshalled the xmlns attribute is being attached to each WorkSet element for some reason, is there a way to not have this attribute assigned when they're being marshalled? – TyC Feb 15 '12 at 16:14
  • That, I am entirely unsure on. I'd start reading through the JAXB javadocs closely I guess. Sorry! – Ophidian Feb 16 '12 at 13:29
  • @TyC do you have any answer for your last question regarding the xmlns attribute? – Dio Apr 16 '15 at 10:55
  • @Dion_E Yes, it was actually answered in a different thread - http://stackoverflow.com/questions/9297872/jaxb-fragment-marshal-w-o-namespace – TyC May 05 '15 at 13:30