Monday, October 15, 2012

JAXB

JAXB  - Java Architecture For XML Binding

JAXB defines an API for reading and writing Java objects to and from XML documents.

http://www.vogella.com/articles/JAXB/article.html

Using schemagen, the JAXB schema generator tool, you can start with a set of Java classes and generate an XML schema.


Using JAXB xjc tooling to generate JAXB classes from an XML schema file

  1. Define an xsd file.
  2. Run xjc against the xsd file
  3. Java class will be generated

Using the JAXB runtime to marshal and unmarshal XML documents

  • Marshal JAXB objects to XML instance documents.
JAXBContext jc = JAXBContext.newInstance("myPackageName");
//Create marshaller
Marshaller m = jc.createMarshaller();
//Marshal object into file.
m.marshal(myJAXBObject, myOutputStream);

  • Unmarshal XML files to JAXB objects.  
JAXBContext jc = JAXBContext.newInstance("myPackageName");
//Create unmarshaller
Unmarshaller um = jc.createUnmarshaller();
//Unmarshal XML contents of the file myDoc.xml into your Java 
  object instance.
MyJAXBObject myJAXBObject = (MyJAXBObject) 
um.unmarshal(new java.io.FileInputStream( "myDoc.xml" ));

No comments:

Post a Comment