JAXB with an example.
What is JAXB?
JAXB stands for Java Architecture for XML Binding. It can be used to convert Java Object to XML and XML back to Java Objects.
With JDK Annotation, it is very simple for implementation. Lets directly jump into implementation.
First, Let us define a Address.java class with basic fields.
package baseproject.jaxb;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* @author
*/
@XmlType
public class Address {
private String street;
private String lane;
private String city;
private String state;
/**
* Get the value of street
*
* @return the value of street
*/
@XmlElement
public String getStreet() {
return street;
}
/**
* Set the value of street
*
* @param street new value of street
*/
public void setStreet(String street) {
this.street = street;
}
/**
* Get the value of lane
*
* @return the value of lane
*/
@XmlElement
public String getLane() {
return lane;
}
/**
* Set the value of lane
*
* @param lane new value of lane
*/
public void setLane(String lane) {
this.lane = lane;
}
/**
* Get the value of city
*
* @return the value of city
*/
@XmlElement
public String getCity() {
return city;
}
/**
* Set the value of city
*
* @param city new value of city
*/
public void setCity(String city) {
this.city = city;
}
/**
* Get the value of state
*
* @return the value of state
*/
@XmlElement
public String getState() {
return state;
}
/**
* Set the value of state
*
* @param state new value of state
*/
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "Address: {" + "street=" + street + ", lane=" + lane + ", city=" + city + ", state=" + state + '}';
}
}
Now, let us define a new Class Employee.java with basic fields and also the 'Address' object.
package baseproject.jaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author
*/
@XmlRootElement
public class Employee {
private String id;
private String name;
private String company;
private Address address;
/**
* Get the value of id
*
* @return the value of id
*/
@XmlElement
public String getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(String id) {
this.id = id;
}
/**
* Get the value of name
*
* @return the value of name
*/
@XmlElement
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@XmlElement
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee: {" + "id=" + id + ", name=" + name + ", company="
+ company + ", address=" + address + '}';
}
}
Now, define the class which converts an Object to XML. For that first we create an object 'Employee' class and set the attributes for the same.
package baseproject.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
/**
*
* @author
*/
public class JAXBObjectToXML {
public static void main(String args[]) {
// First define the address of the employee.
Address address = new Address();
address.setStreet("Main Street");
address.setLane("Main Lane");
address.setCity("Bangalore");
address.setState("Karnataka");
// Define the employee object
Employee e = new Employee();
e.setId("1");
e.setName("Sunil");
e.setCompany("Sunil Systems.");
e.setAddress(address); // Setting employee address object.
try {
File file = new File("src/baseproject/jaxb/emp.xml");
// Now the JAXB
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(e, file);
marshaller.marshal(e, System.out); // displays the output on screen for System.out
} catch (JAXBException ex) {
System.out.println("JAXB Exception: " + ex);
}
}
}
Now, we shall define a JAXBXmlToObject.java class which constructs an object from an XML file. In the following class, we are reading 'emp.xml' file and converting it to 'Employee' object.
package baseproject.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @author
*/
public class JAXBXmlToObject {
public static void main(String args[]) {
try {
File file = new File("src/baseproject/jaxb/emp.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
Employee e = (Employee) unmarhsaller.unmarshal(file);
System.out.println(e);
} catch (JAXBException ex) {
System.out.println("Jaxb Exception... " + ex);
}
}
}
For the same, we shall define XML file similar to one we want for our objects.
Input XML file: emp.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>2nd lane</lane>
<state>Karnataka</state>
<street>2nd Street</street>
</address>
<id>2</id>
<name>Sunil</name>
</employee>
Upon successful run, we shall have 'Employee' lobject being constructed with values in the XML file. Following output confirms the same.
Employee: {id=2, name=Sunil, company=Sunil Systems., address=Address: {street=2nd Street, lane=2nd lane, city=Bangalore, state=Karnataka}}
Please note the output of address object.
Even if the XML contains additional elements for which the attributes does not exists in the class, they would be simply ignored.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>2nd lane</lane>
<state>Karnataka</state>
<street>2nd Street</street>
<cross>2nd cross</cross> <!-- Will be ignored -->
</address>
<id>2</id>
<name>Sunil</name>
<dummy>Dummy, will not be used</dummy> <!-- Will be ignored -->
</employee>
The output would still look the same as above.
Employee: {id=2, name=Sunil, company=Sunil Systems., address=Address: {street=2nd Street, lane=2nd lane, city=Bangalore, state=Karnataka}}
Similarly, if the elements are missing for the attribute in the class, then their default values would be used.
Input XML: emp.xml. (Please see that the element <name/> and <street /> are missing.)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>2nd lane</lane>
<state>Karnataka</state>
</address>
<id>2</id>
</employee>
What is JAXB?
JAXB stands for Java Architecture for XML Binding. It can be used to convert Java Object to XML and XML back to Java Objects.
With JDK Annotation, it is very simple for implementation. Lets directly jump into implementation.
First, Let us define a Address.java class with basic fields.
package baseproject.jaxb;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* @author
*/
@XmlType
public class Address {
private String street;
private String lane;
private String city;
private String state;
/**
* Get the value of street
*
* @return the value of street
*/
@XmlElement
public String getStreet() {
return street;
}
/**
* Set the value of street
*
* @param street new value of street
*/
public void setStreet(String street) {
this.street = street;
}
/**
* Get the value of lane
*
* @return the value of lane
*/
@XmlElement
public String getLane() {
return lane;
}
/**
* Set the value of lane
*
* @param lane new value of lane
*/
public void setLane(String lane) {
this.lane = lane;
}
/**
* Get the value of city
*
* @return the value of city
*/
@XmlElement
public String getCity() {
return city;
}
/**
* Set the value of city
*
* @param city new value of city
*/
public void setCity(String city) {
this.city = city;
}
/**
* Get the value of state
*
* @return the value of state
*/
@XmlElement
public String getState() {
return state;
}
/**
* Set the value of state
*
* @param state new value of state
*/
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "Address: {" + "street=" + street + ", lane=" + lane + ", city=" + city + ", state=" + state + '}';
}
}
Now, let us define a new Class Employee.java with basic fields and also the 'Address' object.
package baseproject.jaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author
*/
@XmlRootElement
public class Employee {
private String id;
private String name;
private String company;
private Address address;
/**
* Get the value of id
*
* @return the value of id
*/
@XmlElement
public String getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(String id) {
this.id = id;
}
/**
* Get the value of name
*
* @return the value of name
*/
@XmlElement
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@XmlElement
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee: {" + "id=" + id + ", name=" + name + ", company="
+ company + ", address=" + address + '}';
}
}
Now, define the class which converts an Object to XML. For that first we create an object 'Employee' class and set the attributes for the same.
package baseproject.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
/**
*
* @author
*/
public class JAXBObjectToXML {
public static void main(String args[]) {
// First define the address of the employee.
Address address = new Address();
address.setStreet("Main Street");
address.setLane("Main Lane");
address.setCity("Bangalore");
address.setState("Karnataka");
// Define the employee object
Employee e = new Employee();
e.setId("1");
e.setName("Sunil");
e.setCompany("Sunil Systems.");
e.setAddress(address); // Setting employee address object.
try {
File file = new File("src/baseproject/jaxb/emp.xml");
// Now the JAXB
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(e, file);
marshaller.marshal(e, System.out); // displays the output on screen for System.out
} catch (JAXBException ex) {
System.out.println("JAXB Exception: " + ex);
}
}
}
Once you run the JAXBObjectToXML, we see the following output.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>Main Lane</lane>
<state>Karnataka</state>
<street>Main Street</street>
</address>
<id>1</id>
<name>Sunil</name>
</employee>
Similarly, It is very easy to convert an XML to an object.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>Main Lane</lane>
<state>Karnataka</state>
<street>Main Street</street>
</address>
<id>1</id>
<name>Sunil</name>
</employee>
Similarly, It is very easy to convert an XML to an object.
Now, we shall define a JAXBXmlToObject.java class which constructs an object from an XML file. In the following class, we are reading 'emp.xml' file and converting it to 'Employee' object.
package baseproject.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
/**
*
* @author
*/
public class JAXBXmlToObject {
public static void main(String args[]) {
try {
File file = new File("src/baseproject/jaxb/emp.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
Employee e = (Employee) unmarhsaller.unmarshal(file);
System.out.println(e);
} catch (JAXBException ex) {
System.out.println("Jaxb Exception... " + ex);
}
}
}
For the same, we shall define XML file similar to one we want for our objects.
Input XML file: emp.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>2nd lane</lane>
<state>Karnataka</state>
<street>2nd Street</street>
</address>
<id>2</id>
<name>Sunil</name>
</employee>
Upon successful run, we shall have 'Employee' lobject being constructed with values in the XML file. Following output confirms the same.
Employee: {id=2, name=Sunil, company=Sunil Systems., address=Address: {street=2nd Street, lane=2nd lane, city=Bangalore, state=Karnataka}}
Please note the output of address object.
Even if the XML contains additional elements for which the attributes does not exists in the class, they would be simply ignored.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>2nd lane</lane>
<state>Karnataka</state>
<street>2nd Street</street>
<cross>2nd cross</cross> <!-- Will be ignored -->
</address>
<id>2</id>
<name>Sunil</name>
<dummy>Dummy, will not be used</dummy> <!-- Will be ignored -->
</employee>
The output would still look the same as above.
Employee: {id=2, name=Sunil, company=Sunil Systems., address=Address: {street=2nd Street, lane=2nd lane, city=Bangalore, state=Karnataka}}
Similarly, if the elements are missing for the attribute in the class, then their default values would be used.
Input XML: emp.xml. (Please see that the element <name/> and <street /> are missing.)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
<address>
<city>Bangalore</city>
<lane>2nd lane</lane>
<state>Karnataka</state>
</address>
<id>2</id>
</employee>
and the output would be:
Employee: {id=2, name=null, company=Sunil Systems., address=Address: {street=null, lane=2nd lane, city=Bangalore, state=Karnataka}}
From the output, we can see that both 'name' and 'street' values are 'null' which are their default values.
No comments:
Post a Comment