JAXB introduction
################################################################
Java Architecture for XML Binding (JAXB)
By Ed Ort and Bhakti Mehta, March 2003
http://www.oracle.com/technetwork/articles/javase/index-140168.html
################################################################
XML and Java technology are natural partners in helping developers exchange data and programs across the Internet.
That's because XML has emerged as the standard for exchanging data across disparate systems,
and Java technology provides a platform for building portable applications.
Q:
How do you access and use an XML document (that is, a file containing XML-tagged data) through the Java programming language?
A:
One way to do this, perhaps the most typical way, is through parsers that conform to the Simple API for XML (SAX) or the Document Object Model (DOM). Both of these parsers are provided by Java API for XML Processing (JAXP).
Now developers have another Java API at their disposal that can make 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 V 1.1
Binding:
Binding a schema means generating a set of Java classes that represents the schema.
All JAXB implementations provide a tool called a binding compiler to bind a schema.
For example:
xjc.sh -p test.jaxb books.xsd -d work
for this command, the classes are packaged in test.jaxb within the work directory.
***Note*** that these classes are implementation-specific.
Because the classes are implementation-specific,
classes generated by the binding compiler in one JAXB implementation will probably not work with another JAXB implementation.
Unmarshal the Document:
Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document.
The content objects are instances of the classes produced by the binding compiler.
For example:
JAXBContext jc = JAXBContext.newInstance("test.jaxb");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Collection collection = (Collection) unmarshaller.unmarshal(new File( "books.xml"));
Another Example: Building an XML Document
If you use DOM to access data, you create a parser that builds a tree, and then you use DOM methods to navigate to the appropriate object in the tree that contains the data you need. So an understanding of the tree's organization is a requirement.
Using JAXB, you would:
1. Bind the schema for the XML document (if it isn't already bound).
2. Create the content tree.
3. Marshal the content tree into the XML document.
Marshal the Content Tree
Marshalling is the opposite of unmarshalling. It creates an XML document from a content tree.
For example:
JAXBContext jaxbContext = JAXBContext.newInstance("test.jaxb");
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
marshaller.marshal(collection, new FileOutputStream("jaxbOutput.xml"));
浙公网安备 33010602011771号