Wednesday, September 16, 2015

Bài tập IXJ (JAXB)

JAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file.
In this tutorial, we show you how to use JAXB to do following stuffs

  1. Marshalling – Convert a Java object into a XML file.

  2. Unmarshalling – Convert XML content into a Java Object.


Working with JAXB is easy, just annotate object with JAXB annotation, later use jaxbMarshaller.marshal() or jaxbMarshaller.unmarshal() to do the object / XML conversion.

Create Java class
[sourcecode language="java"]
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

// Customer will become root node in XML file
@XmlRootElement
public class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
//Name as tag
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
// Age as tag
@XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
// id as attribute of customer tag
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
[/sourcecode]

Convert Object to XML: JAXB marshalling example, convert customer object into a XML file. The jaxbMarshaller.marshal() contains a lot of overloaded methods, find one that suit your output.
[sourcecode language="java"]
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBExample {
public static void main(String[] args) {
// create customer object
Customer customer = new Customer();
customer.setId(100);
customer.setName("nvmit");
customer.setAge(29);
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// convert to XML and write to file
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
[/sourcecode]
Run this code, we have XML as
[sourcecode language="xml"]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>29</age>
<name>nvmit</name>
</customer>
[/sourcecode]

Convert XML to Object: JAXB unmarshalling example, convert a XML file content into a customer object. The jaxbMarshaller.unmarshal() contains a lot of overloaded methods
[sourcecode language="java"]
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class JAXBExample {
public static void main(String[] args) {
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
[/sourcecode]

Run this code
Customer [name=nvmit, age=29, id=100]

OK, very simple.

2 comments:

Translate