JAXB, stands for Java Architecture for XML Binding, which can be used to convert a Java object to XML file or a XML document to corresponding Java Object.
Given below are the two terminologies used in Java for the conversions.
Marshalling - Convert a Java object to XML file
Unmarshalling - Convert a XML file to Java object.
Here we have a class called Car which contains object of another class called Wheel. The UML diagram is as below.
Next we need to create Wheel class as below.
Next is the marshalling program.
Run the application and you will see XML content is written to the console as well as in marshall.xml file in current working directory.
Now we will unmarshall the XML file into Java object. The below code performs JAXB unmarshalling.
When running the code it prints the Java object as below.
UnMarshalled Object:Car [wheel=Wheel [manufacturer=Michellin, cost=1000], color=Black]
Given below are the two terminologies used in Java for the conversions.
Marshalling - Convert a Java object to XML file
Unmarshalling - Convert a XML file to Java object.
Here we have a class called Car which contains object of another class called Wheel. The UML diagram is as below.
First we will need to write the class Car as follows.
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement // This annotation is required to indicate that this is the top level class
public class Car {
Wheel wheel;
String color;
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [wheel=" + wheel + ", color=" + color + "]";
}
}
Next we need to create Wheel class as below.
import javax.xml.bind.annotation.XmlType;
@XmlType //indicates that this is one of the inner structures
public class Wheel {
String manufacturer;
int cost;
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
@Override
public String toString() {
return "Wheel [manufacturer=" + manufacturer + ", cost=" + cost + "]";
}
}
Next is the marshalling program.
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBException;
public class Marshall {
/**
* @param args
*/
public static void main(String[] args) {
JAXBContext ctx;
try {
ctx = JAXBContext.newInstance(Car.class);
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Marshal a Car object: 1st to stdout, 2nd to file
Wheel w = new Wheel();
w.setCost(1000);
w.setManufacturer("Michellin");
Car c = new Car();
c.setColor("Black");
c.setWheel(w);
m.marshal(c, System.out);
FileOutputStream out = new FileOutputStream("marshall.xml");
m.marshal(c, out);
out.close();
} catch (JAXBException | IOException e) {
e.printStackTrace();
}
}
}
Run the application and you will see XML content is written to the console as well as in marshall.xml file in current working directory.
Now we will unmarshall the XML file into Java object. The below code performs JAXB unmarshalling.
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class UnMarshall {
/**
* @param args
*/
public static void main(String[] args) {
JAXBContext ctx;
try {
ctx = JAXBContext.newInstance(Car.class);
Unmarshaller u = ctx.createUnmarshaller();
Car c = (Car) u.unmarshal(new File("marshall.xml"));
System.out.println("UnMarshalled Object:"+ c);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When running the code it prints the Java object as below.
UnMarshalled Object:Car [wheel=Wheel [manufacturer=Michellin, cost=1000], color=Black]
Comments
Post a Comment