Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

XML using JAXB API in java

Mon Jul 06, 2009 1:05 am

XML using JAXB API in java

One of the great features available to java developers is the integration between java technology and XML .Usage of the XML as data exchange standard especially in the internet is a great aspect of current systems. Also we have to keep in mind that Java technology and XML is the core of building web services applications.
So, the big question now is how to deal with XML in java? .You can do that throw using parsers Simple API for XML (SAX) or the document object model (DOM). These two parsers are available by Java API for XML processing (JAXP).

Different between SAX and DOM:

In SAX, the parser passes every element in document from the start to the end. Nothing is saved in the memory. Actions is done once data is parsed and nothing in memory manipulation.
In DOM, the parsers create tree of objects whose represent the document organization of data. Here there is a manipulation to data in memory.

Accessing an XML document:
Using the JAXB to access xml file have the following two steps:
- Bind the schema.
- Unmrashal the document.


1. Bind the schema :

1.JPG
Bind the schema
1.JPG (9.14 KiB) Viewed 9022 times


Binding schema means convert data from xml format to java format as a set of classes whose represents the xml schema.
For example consider we have xml schema for student (student.xml). The generated files are:
CollectionType.java
Collection.java
StudentType.java
ObjectFactory.java


2. Unmarshal the document :


Unmarshaling the xml document means creating objects of the xml organization. Objects are instances of classes generated of binding the sechma .

2.JPG
Unmarshal the document
2.JPG (7.25 KiB) Viewed 9020 times


Steps to unmarshal
1- Create a JAXBContext object. This object is the door JAXB utility.
Code:
JAXBContext jaxbcontextobj = JAXBContext.newInstance("packagetest.jaxb");


2- Create Unmarshaller Object.
Code:
Unmarshaller unmarshaller = jaxbcontextobj.createUnmarshaller();


3- Call the Unmarshal method.
Code:
Collection collection= (Collection)
   unmarshaller.unmarshal(new File( "student.xml"));





4- Use the get method.
Code:
CollectionType.StudentType studentsType = collection.getStudents();
List studentList = studentsType.getStudent();



5- Validation.
One of the important features of JAXB is to allow validation you xml document against its schema (XSD).
Code:
   unmarshaller.setValidating(true);


Marshal the Content Tree
3.JPG
Marshal the Content Tree
3.JPG (7.7 KiB) Viewed 9014 times

After you make changes in the objects content tree, you need to update the xml document. Marshal is the opposite of the unmarshal.



1- Create a JAXBContext object.
Code:
JAXBContext jaxbContextObj = JAXBContext.newInstance("packagetest.jaxb");


2- Create a Marshaller object.

Code:
Marshaller marshallerObj = jaxbContextObj.createMarshaller();


3- Call the marshal method.

Code:
marshallerObj.marshal(collection,
   new FileOutputStream("Output.xml"));


Note : the validation is deferent from the unmashalling , no setValidating ,here we use Validator class .

Code:
Validator validator = jaxbContextObj.createValidator();
validator.validate(collection));


For JAXB examples :
jaxb/




Advantages of using JAXB:


1. Easier access to XML document.
2. Efficient memory usage.
3. High flexibility.

Please reply with your comments about this article .



Post a reply
  Related Posts  to : XML using JAXB API in java
 Unmarshall From XML using JAXB     -  
 update an xml file with jaxb     -  
 how to use enum in jaxb unmarshalling     -  
 insert a namespace in a tag with jaxb     -  
 update an xml file with jaxb and accessing to its elements     -  
 adding an xmlns attribute xith jaxb     -  
 problem with binding a choice model with jaxb     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Java course     -  

Topic Tags

Java XML, Java JAXB