Java Reference
In-Depth Information
Discussion
JAXB requires a Schema (see Verifying Structure with Schema or DTD ) document to work;
this document is a standard schema that describes how to write the fields of your object into
XML or how to recognize them in an incoming XML document.
In Example 20-1 , we'll serialize a Configuration document, a subset of the information a
multiuser app needs to keep track of about each user. We've already annotated this class with
some JAXB annotations, which we'll discuss after the code.
Example 20-1. Configuration.java
/**
* Demo of XML via JAXB; meant to represent some of the (many!)
* fields in a typical GUI for user<-->application configuration
* (it is not configuring JAXB; it is used to configure a larger app).
*/
@XmlAccessorType ( XmlAccessType . FIELD )
@XmlType ( name = "configuration" ,
propOrder ={ "screenName" , "webProxy" , "verbose" , "colorName" })
@XmlRootElement ( name = "config" )
public
public class
class Configuration
Configuration {
private
private String webProxy ;
private
private boolean
boolean verbose ;
private
private String colorName ;
private
private String screenName ;
public
public String getColorName () {
return
return colorName ;
}
public
public void
void setColorName ( String colorName ) {
this
this . colorName = colorName ;
}
// Remaining accessors, hashCode/equals(), are uninteresting.
The Configuration class has four fields, and we want them written in a particular order.
Normally JAXB would find the fields in Reflection (see Chapter 23 ) order, which isn't well
defined. So we list them in the first annotation (these are all from
javax.xml.bind.annotation ):
@XmlType ( name = "configuration" ,
propOrder ={ "screenName" , "webProxy" , "verbose" , "colorName" })
Search WWH ::




Custom Search