Derek
Derek

Reputation: 11915

Java objects to XML using javax.xml.bind.annotations

If I have an object that contains a few fields that are Lists of other objects, and I want to generate the XML to represent that object, I get something like this

<top level object>
  <object1 />
  <object1 />
  <object1 /> 
  <object2 />
  <object2 />
  <object3 />
</top level object>

and I want to generate something like this:

<top level object>
  <object1 list>
    <object1 />
    <object1 />
    <object1 /> 
  </object1 list>
  <object2 list>
    <object2 />
    <object2 />
  </object2 list>
  <object3 />
</top level object>

Is this possible by modifying my annotations? Right now I have my top level class object marked with

 @XmlRootElement()

and each of the Object1, Object2...etc getters (that returns a List < Object#> )has the

@XmlElement()

annotation.

Hopefully there is a way to change my output by modifying the annotations, because it seems silly to me to make a "Object1List" object to simply hold a getter for the other lists to make this work the way I am picturing in my head

Thanks!

Upvotes: 2

Views: 515

Answers (1)

D.Shawley
D.Shawley

Reputation: 59563

Take a look at XmlElementWrapper on java.net or in the Java 6 API. I think that it is exactly what you are after.

Upvotes: 3

Related Questions