Marshaling a property as xml attribute

By default when marshaling to xml file, all properties are marshaled as elements. This default behaviour can be changed and this post explains how to do it.

In this post I will marshall the below java bean class, and id property as its attribute.

Country


1  package package1;
2  
3  import javax.xml.bind.annotation.XmlAttribute;
4  import javax.xml.bind.annotation.XmlRootElement;
5  
6  @XmlRootElement
7  public class Country {
8   private int id;
9      private String name;
10     private int population;
11 
12     @XmlAttribute
13     public int getId() {
14      return id;
15  }
16  public void setId(int id) {
17      this.id = id;
18  }
19  public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public int getPopulation() {
26         return population;
27     }
28     public void setPopulation(int population) {
29         this.population = population;
30     }
31 
32     @Override
33     public String toString() {
34         StringBuilder sb = new StringBuilder();
35         sb.append("{");
36         sb.append("name:").append(this.name).append(",");
37         sb.append("population:").append(this.population).append("}");
38 
39         return sb.toString();
40     }
41 }

Since I want the id property as an attribute, I mark the getter method with XmlAttribute annotation. Any getter annotated with this annotation will be stored as element’s attribute.

Main Code


package package1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

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

public class XMLAttributeDemo {
    public static void main(String[] args) throws JAXBException, IOException {
        JAXBContext jaxbContext = JAXBContext.newInstance(Country.class);

        Marshaller marshaller = jaxbContext.createMarshaller();
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Country country = new Country();
        country.setId(1);
        country.setName("India");
        country.setPopulation(10000000);

        File file = new File("result.xml");
        FileWriter fw = new FileWriter(file);

        marshaller.marshal(country, fw);
    }
}

Output

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<country id=”1″>
<name>India</name>
<population>10000000</population>
</country>

Explanation

From the output we can see that id property is marshalled as attribute of country element instead of an element itself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s