Monday 4 November 2013

How to canvert java object into xml file (Vice Versa)

JAXB, stands for Java Architecture for XML Binding, using JAXB annotation we can convert Java object into XML file and Xml file into java object (Vice versa).
No extra jaxb libraries are required if you are using JDK1.6  


For object that need to convert to / from XML file, it have to annotate with JAXB annotation

//---------------- Save as Customer.java ------------------------

package com.ramsiscode.jaxb;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Customer {

int id;
String name;
int age;
String address;
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
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 getAddress() {
return address;
}
@XmlElement
public void setAddress(String address) {
this.address = address;
}

}


//-----------------------------------------------------------

JAXB marshalling example, for convert customer object into a XML file

//------------------- save as ConvertXML.java -------------------------



package com.ramsiscode.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;


public class ConvertXML {

public static void main(String args[]){

Customer customer = new Customer();
customer.setId(101);
customer.setName("Aman");
customer.setAge(28);
customer.setAddress("Noida, Uttar Pradesh India.");

try{

File file = new File("D:\\customer.XML");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);

}
catch (Exception ex){

ex.printStackTrace();

}

}

}


//----------------- output -----------------------------------




//------------------------------------------------------------------------------
JAXB unmarshalling example, convert a XML file content into a customer object

//------------------------- save as CustomerXMLRead.java ------------------------

package com.ramsiscode.jaxb;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;


public class CustomerXMLRead {

public static void main(String args[]){

try {

File file = new File("D:\\customer.XML");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.getName());
System.out.println(customer.getId());
System.out.println(customer.getAge());
System.out.println(customer.getAddress());


} catch (JAXBException e) {
e.printStackTrace();
}

}

}

 



No comments:

Post a Comment