Magazine
 
Java Architecture for XML Binding

Today, XML has emerged as the standard for exchanging data across disparate systems, and Java technology provides a platform for building portable applications. They partners naturally in helping developers to exchange data and programs across the Internet.

Together, they are the most ideal building blocks to develop Web services and Applications accessing web services. This partnership is particularly important for implementing Web services, which provides the users and the application developers, the program functionality on demand from anywhere to anywhere on the Web.

But how do you couple these technologies together in practice? More specifically, what matters much is the issue like how to access and use an XML document (that is, a file containing XML-tagged data) through the Java programming language.

One way to do this is through parsers that conform to the Simple API for XML (SAX) or the Document Object Model (DOM), which is perhaps the most typical way. Java API for XML Processing (JAXP) provides both of these parsers. Java developers invoke a SAX or DOM parser in an application through the JAXP API to parse an XML document — that is, scan the document and logically break it up into discrete pieces. The parsed content is then made available to the application.

Now developers have another Java API which makes it easier to access XML documents: Java Architecture for XML Binding (JAXB).

A Reference Implementation of the API is now available in the Java Web Services Developer Pack 2.0 which makes it easier to access XML documents from applications written with Java.

Java Architecture for XML Binding (JAXB) allows java developer for mapping between the Java classes and the XML representations. JAXB allow us for marshalling of java objects into XML and vice-versa i.e. unmarshalling of XML documents back into the java objects. Or we can say JAXB enables us to store and retrieve the data in any XML format into the memory.

 

Storing and retrieving data from the memory does not require any implementation to a specific set of XML. JAXB is useful in situations where specification is complex and changing. Regularly changing XML schema definitions can be time consuming and error prone as they keep the XML schema definitions synchronized with the java definitions.

Suppose one need to develop a Java application that accesses and displays data in XML documents.

One approach could be to use the SAX or DOM parsers to access an XML document and then display the data.

In that case, the user would need to:

• Write a program that creates a SAX parser and then uses that parser to parse the XML document. The SAX parser starts at the beginning of the document. When it encounters something significant (in SAX terms, an “event”) such as the start of an XML tag, or the text inside of a tag, it makes that data available to the calling application.

• Create a content handler that defines the methods to be notified by the parser when it encounters an event. These methods, known as callback methods, take the appropriate action on the data they receive.

Now let’s look at how you use JAXB to access an XML document

Oct 2007 | Java Jazz Up | 46
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF