tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 XML > JAXB > Serialize List to XML

Serialize List to XML 

The following example shows serializing a POJO with multiple lists to an XML. The POJO should have a no-arg default constructor and @XmlRootElement annotation at class level. The annotation @XmlElementWrapper allows us to provide a wrapper tag name around list of elements.

File Name  :  
com/bethecoder/tutorials/jaxb/common/StudentClass.java 
   
package com.bethecoder.tutorials.jaxb.common;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD//Allows to get data through fields rather than public getter/setters
public class StudentClass {

  private String name;
  
  @XmlElementWrapper(name = "subjects_list")
  @XmlElement(name="subject")
  private List<String> subjects = new ArrayList<String>();
  
  @XmlElementWrapper(name = "student_list")
  @XmlElement(name="student")
  private List<Student> students = new ArrayList<Student>();
  
  public StudentClass() {
  }

  public StudentClass(String name) {
    super();
    this.name = name;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public List<Student> getStudents() {
    return students;
  }
  public void setStudents(List<Student> students) {
    this.students = students;
  }

  public List<String> getSubjects() {
    return subjects;
  }

  public void setSubject(List<String> subjects) {
    this.subjects = subjects;
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/common/Student.java 
   
package com.bethecoder.tutorials.jaxb.common;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student {

  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;
  
  /**
   * NOTE : Should have a no-arg default constructor.
   */
  public Student() {
  }

  public Student(String firstName, String lastName, int age, String hobby,
      Date dob) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.hobby = hobby;
    this.dob = dob;
  }
  
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  public Date getDob() {
    return dob;
  }
  public void setDob(Date dob) {
    this.dob = dob;
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/tests/Lists.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.jaxb.tests;

import java.util.Arrays;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.bethecoder.tutorials.jaxb.common.Student;
import com.bethecoder.tutorials.jaxb.common.StudentClass;

public class Lists {

  public static void main (String [] argsthrows JAXBException {
    
    Student student = new Student("Sriram""Kasireddi"2"Painting"new Date());
    Student student2 = new Student("Sudhakar""Kasireddi"26"Coding"new Date());
    StudentClass stdCls = new StudentClass("MPC");
    stdCls.getSubjects().addAll(Arrays.asList("Maths""Physics""Chemistry"));
    stdCls.getStudents().addAll(Arrays.asList(student, student2));
      
    /**
     * Create JAXB Context from the classes to be serialized
     */
    JAXBContext context = JAXBContext.newInstance(StudentClass.class);  
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  
    m.marshal(stdCls, System.out);  

  }
}
   

It gives the following output,
File Name  :  OUTPUT


                    Print

   



Books Recommendation

 
 
bl  br