Java Reference
In-Depth Information
or be derived from a schema by JAXB. It might also contain package-level annotations inten-
ded for processing. The second constructor accepts an array of classes that you want JAXB
to marshal or unmarshal. Note that you don't have to specify all classes in a composition; it's
enough to specify the composing class, and JAXB will do the work of marshaling all the rest
of them.
Example 3-7 is a basic example of how to perform a marshal and print to the console to see
the result. In order to illustrate that JAXB can work with object compositions out of the box,
you'll make an example Book class to marshal that is composed of an Author type, a Category
type, a title string, and a double price.
Example3-7.Book class to marshal into XML
package com.soacookbook.ch02.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Book {
private String title;
private double price;
private Author author;
private Category category;
//... getters and setters omitted.
}
class Author {
private String firstName;
private String lastName;
//... getters and setters omitted.
}
enum Category {
LITERATURE,
PHILOSOPHY,
PROGRAMMING
;
}
Example 3-8 illustrates how to marshal an instance of your Book class to XML. The result is
sent to an output stream, which is, in this case, just the standard out.
Example3-8.Using JAXB to marshal a Book object into XML and print it to the console
Search WWH ::




Custom Search