JAXB (Java Architecture for XML Binding ) is a Java standard that defines how Java objects can be converted to XML (Marshalling in jaxb terms) and
the other way around (UnMarshalling).
No extra jaxb libraries are required if you are using JDK1.6 or above, because
JAXB is bundled in JDK 1.6.
Note : -For JDK < 1.6, download JAXB from
here, and puts “
jaxb-api.jar” and “
jaxb-impl.jar” on your project classpath.
JAXB uses annotations to indicate the central elements.
@XmlRootElement(namespace = "namespace") : Define the root element for a XML tree
@XmlType(propOrder = { "field2", "field1",.. }): Allows to define the order in which the fields are written in the XML file
@XmlElement(name = "newName"): Define the XML element which will be used. Only need to be used if the newName is different than the javabean name
@XmlAccessorType(XmlAccessType.FIELD) :Used to show the feilds in xml file
@XmlAttribute : Define the attribute for an XML element
Lets analyse a Student model .
Student.java
package com.opensourzesupport.jaxb.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author prasobh
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
/ /If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "age", "name", "address"})
public class Student {
@XmlAttribute
private long id;
// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name="studentname")
private String name;
@XmlElement
private int age;
@XmlElement
private Address address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" + "id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + '}';
}
}
Address.java
package com.opensourzesupport.jaxb.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author prasobh
*/
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" + "address=" + address + '}';
}
}
Now lets write a helper class for converting object to xml and vise versa.
Convertor.java
package com.opensourzesupport.jaxb.codec;
import com.opensourzesupport.jaxb.constants.JAXBContants;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
/**
*
* @author prasobh
*/
public class Convertor {
JAXBContext jAXBContext = null;
public void init() throws JAXBException {
jAXBContext = JAXBContext.newInstance(JAXBContants.JAXB_CLASSES);
}
public String encode(Object obj) throws JAXBException {
StringWriter writer = new StringWriter();
jAXBContext.createMarshaller().marshal(obj, writer);
return writer.toString();
}
public Object decode(String xml) throws JAXBException {
StringReader reader = new StringReader(xml);
return jAXBContext.createUnmarshaller().unmarshal(reader);
}
}
JAXBContants class holds all the requred modesl for marshalling and unmarshalling
JAXBContants.java
package com.opensourzesupport.jaxb.constants;
import com.opensourzesupport.jaxb.model.Student;
/**
*
* @author prasobh
*/
public class JAXBContants {
public static Class[] JAXB_CLASSES = {Student.class};
}
Let's test our application.
package com.opensourzesupport.jaxb;
import com.opensourzesupport.jaxb.codec.Convertor;
import com.opensourzesupport.jaxb.model.Address;
import com.opensourzesupport.jaxb.model.Student;
/**
*
* @author prasobh
*/
public class TestJAXB {
public static void main(String[] args) {
Student student = new Student();
Address address = new Address();
student.setName("Prasobh K");
student.setId(System.nanoTime());
student.setAge(26);
address.setAddress("OpenSourzeSupport");
student.setAddress(address);
Convertor convertor = new Convertor();
try {
convertor.init();
String xml = convertor.encode(student);
System.out.println("Encode value ["+ xml +"]");
Object studObj = convertor.decode(xml);
System.out.println("Decoded student obj["+studObj+"]");
} catch (Exception e) {
e.printStackTrace();;
}
}
}
Output
Encode value :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student id="2441156348740">
<studentname>Prasobh K</studentname>
<age>26</age>
<address>
<address>OpenSourzeSupport</address>
</address>
</student>
Decoded student obj[
Student{id=2441156348740, name=Prasobh K, age=26, address=Address{address=OpenSourzeSupport}}
]
Thats it.Enjoy coding..
No comments:
Post a Comment