JAXB Interview Questions and Answers - Unmarshalling
Q. What does JAXB stand for? What is an XML Binding?
A. JAXB means Java API for Xml Binding. XML binding maps an XML to in-memory Java objects. The principle advantage of using JAXB when marshalling and demarshalling XML is that is simplifies the programming model by allowing us to simply annotate a few POJOs and use the JAXB API’s and you can serialise to XML and deserialise from XML very easily. This makes it much simpler than alternatives such as DOM and SAX.
Q. Why use JAXB?
A. JAXB is a reference interface for which you can have a number of different implementations like default reference implementation shipped JDK6, JaxMeAPI, MOXy, Metro, etc. JAXB is available in JDK6 onwards, so it doesn't require any external library and it doesn't require a XML schema to work. XML schema is optional. Easy to use as you can use annotations on your model classes. It is supported by RESful frameworks like Jersey, Spring MVC, RESTEasy, etc.
Here is an example of Unmarshalling XML to Java object:
Step 1: Sample XML to unmarshall
<employee> <id>123</id> <name>Peter</name> </employee>
Step 2: The in memory Java object class. It requires a default constructor.
package com.mycompany.app12; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Employee { private Integer id; private String name; //JAXB requires a default constructor. private Employee(){} public Employee(Integer id, String name) { super(); this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } }
Step 3: The JAXB unmrshaller utility class that can be used with different object types and XML string.
package com.mycompany.app12; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; public class JAXBUnMarshaller { public Object unmarshalObject(Class<?> classType, String xmlString) { Object object = new Object(); try { StringReader stringReader = new StringReader(xmlString); StreamSource streamSource = new StreamSource(stringReader); JAXBContext jaxbContext = JAXBContext.newInstance(classType); Unmarshaller unMarshaller = jaxbContext.createUnmarshaller(); object = unMarshaller.unmarshal(streamSource); return object; } catch (JAXBException e) { e.printStackTrace(); throw new RuntimeException("Error unmarshalling class : " + classType + "\n" + e.getMessage()); } } }
Step 4: The test class demomnstrating the unmarshalling (i.e. convert XML string to Java object) in action.
package com.mycompany.app12; public class Test { private static final String XML_STRING = "<employee>\r\n" + " <id>123</id>\r\n" + " <name>Peter</name>\r\n" + "</employee>"; public static void main(String[] args) { JAXBUnMarshaller unmashaller = new JAXBUnMarshaller(); Employee unmarshalObject = (Employee) unmashaller.unmarshalObject(Employee.class, XML_STRING); System.out.println(unmarshalObject); } }
Step 5: Finally, the output from the toString( ) method in class Employee.
Person [id=123, name=Peter]
You may also like:
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home