Total members 11890 |It is currently Sat Apr 20, 2024 12:15 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 :

Attachment:
File comment: Bind the schema
1.JPG
1.JPG [ 9.14 KiB | Viewed 8850 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 .

Attachment:
File comment: Unmarshal the document
2.JPG
2.JPG [ 7.25 KiB | Viewed 8848 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
Attachment:
File comment: Marshal the Content Tree
3.JPG
3.JPG [ 7.7 KiB | Viewed 8842 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 .



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 adding an xmlns attribute xith jaxb     -  
 problem with binding a choice model with jaxb     -  
 update an xml file with jaxb and accessing to its elements     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 Java course     -  
 need help in java     -  



Topic Tags

Java JAXB, Java XML
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com