JAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file.
No need of external jar library files. JAXB is bundled in from JDK 1.6.
Basic features :
Marshalling – Convert a Java object into a XML file.
Unmarshalling – Convert XML content into a Java Object.
Model class
create a model class by using jaxb annotation to convert model class into xml file.
Generate/Convert Model object to xml
JAXB marshalling convert customer object into a XML file i.e. jaxbMarshaller.marshal().
Convert XML to Object
JAXB unmarshalling convert a XML file content into a customer object i.e. jaxbMarshaller.unmarshal()
Full source code:
No need of external jar library files. JAXB is bundled in from JDK 1.6.
Basic features :
Marshalling – Convert a Java object into a XML file.
Unmarshalling – Convert XML content into a Java Object.
Model class
create a model class by using jaxb annotation to convert model class into xml file.
Generate/Convert Model object to xml
JAXB marshalling convert customer object into a XML file i.e. jaxbMarshaller.marshal().
Convert XML to Object
JAXB unmarshalling convert a XML file content into a customer object i.e. jaxbMarshaller.unmarshal()
Full source code:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.annotation.XmlAttribute; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
/** | |
* | |
* @author Rivet Systems | |
*/ | |
@XmlRootElement | |
public class Customer { | |
String name; | |
String email; | |
int id; | |
public String getName() { | |
return name; | |
} | |
@XmlElement | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getEmail() { | |
return email; | |
} | |
@XmlElement | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public int getId() { | |
return id; | |
} | |
@XmlAttribute | |
public void setId(int id) { | |
this.id = id; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
/** | |
* | |
* @author Rivet Systems | |
*/ | |
public class JAXBMarshall { | |
public static void main(String[] args) { | |
Customer customer = new Customer(); | |
customer.setId(100); | |
customer.setName("uttesh"); | |
customer.setEmail("uttesh@gmail.com"); | |
try { | |
File file = new File("customer.xml"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); | |
Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); | |
// output pretty printed | |
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
jaxbMarshaller.marshal(customer, file); | |
jaxbMarshaller.marshal(customer, System.out); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Unmarshaller; | |
/** | |
* | |
* @author Rivet Systems | |
*/ | |
public class JAXBUnMarshall { | |
public static void main(String[] args) { | |
try { | |
File file = new File("customer.xml"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); | |
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); | |
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file); | |
System.out.println("Customer Name :"+customer.getName()); | |
System.out.println("Customer Email :"+customer.getEmail()); | |
System.out.println("Customer Id :"+customer.getId()); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
0 comments:
Post a Comment