xjc.sh -p test.jaxb books.xsd -d work
The -p option identifies a package for the generated classes, and the -d option identifies a target directory. So for this command, the classes are packaged in test.jaxb within the work directory.
In response, the binding compiler generates a set of interfaces and a set of classes that implement the interfaces. Here are the interfaces it generates for the books.xsd schema:
* CollectionType.java. Represents the unnamed complex type for the <Collection> element.
* Collection.java. Represents the <Collection> element.
* BookType.java. Represents the BookType complex type.
* ObjectFactory.java. Contains methods for generating instances of the interfaces.
Here are the classes that implement the interfaces (these are generated in an impl subdirectory). Note that these classes are implementation-specific -- in this example, they are specific to the Reference Implementation. Because the classes are implementation-specific, classes generated by the binding compiler in one JAXB implementation will probably not work with another JAXB implementation. So if you change to another JAXB implementation, you should rebind the schema with the binding compiler provided by that implementation.
* impl/CollectionTypeImpl.java. Implements the CollectionType interface described in CollectionType.java.
* impl/CollectionImpl.java. Implements the Collection interface described in Collection.java.
* impl/BookTypeImpl.java. Implements the BookType interface described in BookType.java.
In total, the generated classes represent the entire books.xsd schema. Notice that the classes define get and setmethods that are used to respectively obtain and specify data for each type of element and attribute in the schema.
You then compile the generated interfaces and classes. For example:
javac test/jaxb/*.java test/jaxb/impl/*.java
This compiles all of the interfaces and classes in the test.jaxb package generated by the binding compiler.
Unmarshal the Document
Unmarshalling
Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document. The content tree is not a DOM-based tree. In fact, content trees produced through JAXB can be more efficient in terms of memory use than DOM-based trees.
The content objects are instances of the classes produced by the binding compiler. In addition to providing a binding compiler, a JAXB implementation must provide runtime APIs for JAXB-related operations such as marshalling. The APIs are provided as part of a binding framework. The binding framework comprises three packages. The primary package, javax.xml.bind, contains classes and interfaces for performing operations such as unmarshalling, marshalling, and validation (marshalling and validation will be covered later). A second package, javax.xml.bind.util, contains a number of utility classes. The third package, javax.xml.bind.helper, is designed for JAXB implementation providers.
To unmarshal an XML document, you:
* Create a JAXBContext object. This object provides the entry point to the JAXB API. When you create the object, you need to specify a context path. This is a list of one or more package names that contain interfaces generated by the binding compiler. By allowing multiple package names in the context path, JAXB allows you to unmarshal a combination of XML data elements that correspond to different schemas.
For example, the following code snippet creates a JAXBContext object whose context path is test.jaxb, the package that contains the interfaces generated for the books.xsd schema:
import javax.xml.bind.JAXBContext;
JAXBContext jc = JAXBContext.newInstance("test.jaxb");
* Create an Unmarshaller object. This object controls the process of unmarshalling. In particular, it contains methods that perform the actual unmarshalling operation. For example, the following code snippet creates an Unmarshaller object:
import javax.xml.bind.Unmarshaller;
Unmarshaller unmarshaller = jc.createUnmarshaller();
* Call the unmarshal method. This method does the actual unmarshalling of the XML document. For example, the following statement unmarshals the XML data in the books.xml file:
Collection collection= (Collection)
unmarshaller.unmarshal(new File( "books.xml"));
Note that a Collection here is a test.jaxb.Collection, not a java.util.Collection.
* Use the get methods in the schema-derived classes to access the XML data. Recall that the classes that a JAXB compiler generates for a schema include get and set methods you can use to respectively obtain and specify data for each type of element and attribute in the schema. For example, the following statement gets the data in the books and book elements:
CollectionType.BooksType booksType = collection.getBooks();
List bookList = booksType.getBook();
After obtaining the data, you can display it directly from your program. Here, for example, is a program that unmarshals the data in the books.xml file and then displays the data. If you run the program, you should see the following result:
Book details
Item id: 999
Book Name: Learning JAXB
Book ISBN: 123445
Book Price: 34 $
Book category: other
Book promotion: 10% on this book if purchased by March 2003
No of Authors 1
Author Name Jane Doe
Book details
Item id: 129
Book Name: Java Webservices today and Beyond
Book ISBN: 522965
Book Price: 29 $
Book category: magazine
Book promotion: Buy one get Learning webservices Part 1 free
No of Authors 2
Author Name John Brown
Author Name Peter T.