JAXB : jaxb and collections

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

JAXB and collections

In the previous part of this series, we learned about Marshalling and Unmarshalling using JAXB for simple objects. In this part we are going to see how to deal with jaxb and collections.

Consider we have a scenario, we have multiple bikes and these bikes are available in a showroom. The showroom needs to have all the information about all the bikes it has. We want to print this information in the xml file in a readable format. As described in the previous post, we have Bike class .

Now we want a ShowRoom class which has bikes. So lets create it

What do the annotations used here mean?

  1. @XmlRootElement : Defines the root element of xml file to be generated. We have annotated ShowRoom class with it so root element will be showroom
  2. @XmlType(propOrder ={ "name", "id", "bikes" }): Here we are defining the order of child nodes of showroom tag. So name will appear first and bikes will be last.
  3. @XmlElement: Defines the element to be written in xml
  4. @XmlAttribute: Defines the attribute to be written in xml.
  5. @XmlElementWrapper: Defines the wrapper element when using jaxb with collections.

We have multiple bikes in the showroom and hence we would like to list them in separate wrapper element.

  1. Create some Bike objects
  2. Create a list of bikes
  3. Create ShowRoom object
  4. Set previously created bike list for the showroom.

 

Marshalling

  1. Create a JAXBContext for ShowRoomclass using JAXBContext context = JAXBContext.newInstance(ShowRoom.class);.
  2. Create a Marshaller from context using Marshaller jaxbMarshaller = context.createMarshaller();
  3. Use jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); so that the generated xml is formatted and easy to read.
  4. Finally marshal the ShowRoomusing jaxbMarshaller.marshal(showRoom, showRoomDump); where showRoomDumpis the file object created using File showRoom = new File("./showRoomDump.xml");

 

Unmarshalling

  1. Create Unmarshaller from the context created previously using Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
  2. Unmarshal the xml file into ShowRoomobject using ShowRoom unMarshalledShowRoom = (ShowRoom) jaxbUnmarshaller.unmarshal(showRoomDump);

So we will write a JAXBTest class to try it out as follows

Here we go,

This will generate the xml output as follows

After the file is generated, it is again Unmarshalled and converted into java object which produced the output

Thus, we have learned using marshalling and unmarshalling with jaxb and collections.

Share Button

3 comments for “JAXB : jaxb and collections

  1. haris
    December 3, 2013 at 12:42 pm

    thanks for the tutorial.

    I have a one question.
    why is that element, attribute annotations are in setter method in bike class and in getter method in showroom class please let me know which is correct. ?? where it has to be in ?

    • Prasad Kharkar
      December 3, 2013 at 3:31 pm

      Hi Haris, the annotation can be applied either to getter or setter. It does not make any difference. Either ways are correct.

Leave a Reply

Your email address will not be published. Required fields are marked *