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

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;
	} 
}

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.

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();
	      } 
	}
}

Run this code, we have XML as

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>29</age>
    <name>nvmit</name>
</customer>

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

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();
	  } 
	}
}

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

OK, very simple.

Advertisements

2 thoughts on “Bài tập IXJ (JAXB)

Phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s