4

How can I annotate a Bean for JAXB if I want to use such an XML file?

<myobjects>
    <myobject id="1" active="true">
        <...>
    </myobject>
    <myobject id="2" active="true">
        <...>
    </myobject>
    ...
    <myobject id="666666" active="true">
        <...>
    </myobject>
</myobjects>

...a list/collection of XmlRootElements is to be mapped to...

@XmlRootElement(name="myobject")
    public class XmlMyObjectDTO {
    private long id;

    public long getId() {
        return this.id;
    }
...
}
du-it
  • 2,561
  • 8
  • 42
  • 80

1 Answers1

0

There are a couple of ways to handle this use case.

Option 1 - Introduce a New Top Level Class

You could introduce a new class called XmlMyObjectDTOs that contains a list of MyXmlObjectDTO objects. Alternatively you could write a generic wrapper class (see link to related answer below):

Option 2 - Use JAXB with StAX

You can create an XMLStreamWriter to write out the root element and then marshal the collection of objects to the XMLStreamWriter.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400