In this tutorial we will learn what is the significance of JAXB in Java.
JAXB stands for Java Architecture for XML Binding, which is using to convert java object to/from XML
Marshalling: JAXB provides support to convert java object to XML.
Unmarshalling: JAXB provides support to convert XML to a JAVA Object.
Let’s run a small program using JAXB API.
@XmlRootElement defines the root element of an xml document.
@XmlAttribute defines the attribute of the root element.
@XmlElement defines the sub element of the root element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Employee { private int empId; private String empName; private String empDept; public Employee() {} public Employee(int empId, String empName, String empDept) { super(); this.empId = empId; this.empName = empName; this.empDept = empDept; } @XmlAttribute public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } @XmlElement public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } @XmlElement public String getEmpDept() { return empDept; } public void setEmpDept(String empDept) { this.empDept = empDept; } @Override public String toString() { return "Employee [empId=" + empId + ", empName=" + empName + ", empDept=" + empDept + "]"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class XMLMarshaller { public static void main(String[] args) throws Exception{ Employee e1=new Employee(1,"Sreenath","Development"); //Call marshall method to convert java object to XML. System.out.println("Marshalling : Converting java object into XML ........ "); String xml= marshall(e1, Employee.class); System.out.println(xml); //Call Unmarshal method to convert XML to java object. System.out.println("UnMarshalling : Converting XML to java object. ..... "); Object obj= unmarshall(Employee.class,xml); System.out.println(obj); } public static String marshall(Object obj, Class objectClass) throws JAXBException{ // create JAXB context and initializing Marshaller JAXBContext jaxbCtxt = JAXBContext.newInstance(objectClass); Marshaller jaxbMarshaller = jaxbCtxt.createMarshaller(); // for getting nice formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter stringWriterObj = new StringWriter(); // Converting java object to xml using JAXB marshal API jaxbMarshaller.marshal(obj, stringWriterObj); String xmlString = stringWriterObj.toString(); return xmlString; } public static Object unmarshall(Class objectClass, String xml) throws JAXBException{ // Creating an Unmarshaller JAXBContext jaxbCtxt = JAXBContext.newInstance(objectClass); Unmarshaller jaxbUnmarshaller = jaxbCtxt.createUnmarshaller(); StringReader sr = new StringReader(xml); //Converting XML to java object using JAXB unmarshal API. Object obj = jaxbUnmarshaller.unmarshal(sr); return obj; } } |
OUT PUT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Marshalling : Converting java object into XML ........ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee empId="1"> <empDept>Development</empDept> <empName>Sreenath</empName> </employee> UnMarshalling : Converting XML to java object. ........ Employee [empId=1, empName=Sreenath, empDept=Development] |