Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.
At the very minimum this binding schema must specify a root element. For example, the Shakespearean plays have a root element of <PLAY>. Create a binding schema called Shakespeare.xjs that contains the following three lines:
<xml−java−binding−schema version="1.0ea">
<element name="PLAY" type="class" root="true" />
</xml−java−binding−schema>
Save the file in the same directory as play.dtd. Shakespeare.xjs has a root element called
<xml−java−binding−schema> with the version specified as early−access 1.0. For now, the only other content of this file is the element <element> that has the name attribute set to the play.dtd element <PLAY> with instructions that this element will become a class and that it can be the root element of a document. You can specify that multiple elements can be root elements by using more than one <element> tag.
You can now use play.dtd to generate Java classes by using the schema compiler and passing it the name of the DTD as well as the name of the binding schema. Open a command window and navigate to the directory containing play.dtd and Shakespeare.xjs. Enter this command:
java com.sun.tools.xjc.Main "play.dtd" "Shakespeare.xjs"
This command runs the schema−compiler application and passes in two parameters. The first is for the schema specified, in this case, by play.dtd. The second is for the binding schema Shakespeare.xjs. After a moment you'll see feedback in the console window to let you know that .java files are being generated with the names ACT.java, EPILOGUE.java, FM.java, INDUCT.java, LINE.java, PERSONAE.java, PGROUP.java, PLAY.java, PROLOGUE.java, SCENE.java, and SPEECH.java.
285
Chapter 14: Transforming and Binding Your XML Documents The generated code PLAY.java contains instance variables for each of the contained elements. Any of the elements declared to contain #PCDATA are variables of type String in the generated Java code. The elements (such as <PERSONAE>) that contain other elements become classes in their own right. This means that you end up with a PERSONAE.java file, and that the PLAY class contains a variable named _PERSONAE of type PERSONAE. You'll notice that, because play.dtd allows the element <PLAY> to contain one or more of the
<ACT> elements, the schema compiler generates a class named ACT.java and the PLAY class contains a List named _ACT created using a utility inner class called PredicatedLists. Listing 14−2 shows a piece of the generated source code.
Listing 14−2: Play class source code (excerpt)
// many imports
public class PLAY extends MarshallableRootElement
implements RootElement {
private String _TITLE;
private FM _FM;
private PERSONAE _PERSONAE;
private String _SCNDESCR;
private String _PLAYSUBT;
private INDUCT _INDUCT;
private PROLOGUE _PROLOGUE;
private List _ACT = PredicatedLists.createInvalidating(
this, new ACTPredicate(), new ArrayList());
private PredicatedLists.Predicate pred_ACT =
new ACTPredicate();
private EPILOGUE _EPILOGUE;
// accessor methods begin
public String getTITLE() {...}
public void setTITLE(String _TITLE) {...}
// accessors for FM, PERSONAE, SCNDESCR,PLAYSUBT, INDUCT,
// PROLOGUE, EPILOGUE are same. ACT is different it's a List.
//methods for validation
public void validateThis()
throws LocalValidationException {...}
public void validate(Validator v)
throws StructureValidationException {...}
// marshal methods turn content trees into XML documents
public void marshal(Marshaller m) throws IOException {...}
public void unmarshal(Unmarshaller u)
throws UnmarshalException {...}
// unmarshal methods turn XML files into Java objects
public static PLAY unmarshal(InputStream in)
throws UnmarshalException {...}
public static PLAY unmarshal(XMLScanner xs)
throws UnmarshalException {...}
public static PLAY unmarshal(XMLScanner xs, Dispatcher d)
286
Chapter 14: Transforming and Binding Your XML Documents throws UnmarshalException {...}