Java API for XML Binding (JAXB) is an acronym that stands for Java Architecture for XML Binding. Marshalling and Unmarshalling in Java Objects are used both on the client and server side. It enables you to write Java objects into XML and read them back as objects. Simply said, it allows you to convert Java objects into XML and vice-versa.
JAXB was designed to simplify the process of connecting Java programs with XML data and processing functions.
JAXB makes it simple for Java developers to include XML data and processing operations in their applications by allowing them to bind XML schemas to Java representations.
In this article, we are providing complete information about Marshalling and Unmarshalling Java Objects using JAXB.
Unmarshalling (reading) XML instance documents into Java content is handled by JAXB, before marshaling (writing) Java content back into XML instance documents.
It also enables you to create XML schemas from Java objects.
JAXB – Marshalling/UnMarshaling (Convert Object <—> XML)
Java for XML Binding, Use annotation pattern to convert java objects to or from XML. Here the two terms mostly used are Marshaling and Unmarshaling.
By using a java object, create an xml file or convert a java object to xml.
Create a java object again using an xml file.
Note: If using jdk version less than 1.6, download JAXB from http://jaxb.java.net/, and copy “jaxb-api.jar” and “jaxb-impl.jar” and paste to your classpath.
No extra jaxb libraries are required if you are using JDK1.6 or above because it is bundled with jdk
To convert things to/from XML using JAXB, use the Annotation pattern. We can apply a number of predefined annotation styles to our Java objects.
This annotation does not participate directly in the xml creation; rather, it supplies information about xml schema.
It has various properties like factoryClass, method, name, namespace, propOrder, and so on.
name – Provides name to XML schema if you don’t want to use the class name
namespace – Provides the name of the target namespace (if you are using a reference object of some other class)
propOrder – Used to establish an ordering of the sub-elements
@XmlRootElement(name="TestDoc" )
@XmlType(propOrder = { "name", "stream", "class"})
public class TestingDocument
{
String name;
int class;
String stream;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public String getStream() {
return stream;
}
@XmlElement(name = "subject")
public void setStream(String stream) {
this.stream = stream;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TestDoc>
<age></age>
<subject></subject>
<name></name>
</TestDoc>
The basic annotation for a field that’s intended to be an element is XmlElement. It permits you to define the XML element name and namespace.
In the above example, the property stream is renamed by subject using the name property of the XMLElement annotation.
Top-level element class, Define root element of XML document. Its optional elements are name and namespace. By default, we used the class name as the name.
@XmlRootElement(name="TestDoc" )
public class TestingDocument
{
}
<?xml version="1.0" encoding="UTF-8"?><TestDoc>
</TestDoc>
Here is one more condition that I would like to explain here. Sometimes you need to define property with xml tab like in below xml property subjectType in subject tag-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TestDoc>
<age>25</age>
<subject subjectType="Math" >Science</subject>
<name>testinguser</name>
</TestDoc>
To handle this type of condition, we need to create one more java class that will be used to set have property subjectType and its value, In our example we create a subject class like –
public class Subject {
@XmlValue
private String Subject;
@XmlAttribute(name = "subjectType", required = true)
private String subjectType;
public String getSubject() {
return StandardPrice;
}
public void setSubject(String value) {
StandardPrice = value;
}
public void setSubjectType(String currency) {
this.currency = currency;
}
public String getSubjectType() {
return currency;
}
}
@XmlRootElement(name="TestDoc" )
@XmlType(propOrder = { "name", "stream", "class"})
public class TestingDocument
{
String name;
int class;
Subject stream;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public Subject getStream() {
return stream;
}
@XmlElement(name = "subject")
public void setStream(Subject stream) {
this.stream = stream;
}
}
In the above code we used @XmlValue to denote that this is used to add a property value, not a new xml tag.
If we do not define this, then it will produce one extra <subject> tag within a main <subject> tag. @XmlAttribute(name = “subjectType”, required = true) here we are defining that property name as subjectType and it is a required field.
Method getSubjectType() will return the value of subjectType property and getSubject() method will return the value of the subject tag in XML.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<students>
<student></student>
<student></student>
<student></student>
</students>
package com.tst.javaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TestingDocumentExample {
public static void main(String[] args) {
TestingDocument testDoc = new TestingDocument();
testDoc.setName("testuser");
testDoc.setAge(25);
Subject sub = new Subject();
sub.setSubject("Science");
sub.setSubjectType("Meth");
testDoc.setStream(sub);
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(TestingDocument.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(testDoc, file);
//to show out put in console
jaxbMarshaller.marshal(testDoc, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
package com.tst.javaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TestingDocumentExampleUnMarshal {
public static void main(String[] args) {
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(TestingDocument.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
TestingDocument tstDoc = (TestingDocument) jaxbUnmarshaller.unmarshal(file);
System.out.println(tstDoc);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
We hope this tutorial helped you in some manner. You can share your thoughts in the comments down below. Need expert advice? Meet our team of specialists today!
Marshaling and unmarshaling are necessary for:
Marshaling can impact Java project performance in several ways:
Unmarshaling can impact Java project performance in several ways:
About the Author
Latest Blog