tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 XML > JAXB > Customize XML Tag names using Property Accessor

Customize XML Tag names using Property Accessor 

The following example shows customizing XML tag name using @XmlElement annotation. JAXB uses the following accessor types to serialize java object to XML,

  • PUBLIC_MEMBER (default) - Binds public getters/setters/fields
  • FIELD - Binds non static, non transient fields
  • PROPERTY - Binds public getters/setters
  • NONE - No binding
As the default accessor type is PUBLIC_MEMBER, the annotation(Such as @XmlElement) we put at field level causes Class has two properties of the same name exception. This is because JAXB tries to get data using both fields and getter/setter pairs. We can solve this problem by placing annotation at public getter/setter level using default PUBLIC_MEMBER or PROPERTY accessor type.
 

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

import java.util.Date;

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

@XmlRootElement(name = "student")
//@XmlAccessorType(XmlAccessType.PROPERTY)
public class Student3 {
  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;
  
  /**
   * NOTE : Should have a no-arg default constructor.
   */
  public Student3() {
  }

  public Student3(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;
  }
  
  /**
   * Since the default @XmlAccessorType uses
   * public getter/setters use the same to
   * specify alternate name
   */
  @XmlElement(name = "first_name")
  public String getFirstName() {
    return firstName;
  }
  
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  
  @XmlElement(name = "last_name")
  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/CustomNames.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jaxb.tests;

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

public class CustomNames {

  public static void main (String [] argsthrows JAXBException {
    
    Student3 student = new Student3("Sriram""Kasireddi"2"Painting"new Date());
    
    /**
     * Create JAXB Context from the classes to be serialized
     */
    JAXBContext context = JAXBContext.newInstance(Student3.class);  
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  
    m.marshal(student, System.out);  

  }
}
   

It gives the following output,
File Name  :  OUTPUT



 
  


  
bl  br