XML(eXtensible Markup Language) is a platform and language-independent way of defining tags. It is a very powerful and useful way to interchange data between two different platforms. XML allows us to use meaningful tags to represent data in a structured format.
JAXB stands for Java Architecture for XML Binding. It is a binding framework. JAXB API is used to map XML documents to Java objects or it is helpful to convert XML object to java object. For the conversion of XML to java it also uses serialization and deserialization process. It provides simple API which process the xml string or xml file. JAXB implementation is included in Java SE 6 version.
- As compare to DOM, JAXB uses memory in a very efficient way.
- SAX and DOM are generic XML parsers. They will parse any well-structured XML
- JAXB creates a parser that is specific to your DTD means JAXB parser will parse only valid XML (i.e. defined by our DTD) and it allows to unmarshall XML data coming from any source like URL, InputStream, File or a DOM node.
- DOM and JAXB both produce a tree in memory, DOM produces a generic tree where everything is a Node and JAXB produces a tree of Objects with names and attributes as described by our DTD.
- JAXB allows random processing of XML documents. In DOM, we have to go through a tree structure and JAXB doesn’t require any navigations.
- JAXB applications are specific to a single schema, whereas DOM applications can be managed with multiple schemas.
What is marshalling?
Ans:- The process of producing an XML document from Java objects is known as marshalling.
What is unmarshalling?
Ans:- The process of producing a content tree from an XML document or mapping XML document into java objects is known as unmarshalling.
How to map XML document/ Strings with the Java objects?
Sample code:-
package com.gaurav.xml.examples;
import java.util.Date;
public class StudentBean {
private int studentId;
private String studentName;
private Date birthDate;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
Below code Unmarshal Xml String into Mapped Java Object of StudentBean using JAXB.
package com.gaurav.xml.examples;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class XMLStringConversionIntoJava {
public static void main(String[] args) {
try {
String studentXMLString = "<student><studentId>1234</studentId><studentName>Arnold</studentName><birthDate>1985-11-26</birthDate></student>";
JAXBContext jaxbContext = JAXBContext
.newInstance(StudentBean.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StreamSource streamSource = new StreamSource(new StringReader(
studentXMLString));
JAXBElement<StudentBean> je = unmarshaller.unmarshal(streamSource,
StudentBean.class);
StudentBean studentBean = (StudentBean) je.getValue();
System.out
.println("Student Id is :- " + studentBean.getStudentId());
System.out.println("Student Name is :- "
+ studentBean.getStudentName());
System.out.println("Student Birthdate is :- "
+ studentBean.getBirthDate());
} catch (JAXBException jaxbe) {
System.out.println("");
}
}
}
Result:-
Note:- I am using JDK1.7. So for the execution of above program,no additional jars are required.
No comments:
Post a Comment