JAXBWhat is JAXB?Advantages and disadvantagesHow JAXB worksA first exampleAdding complexityImproving the binding schemaMarshallingLimitations of JAXBA minimal binding schemaMore complex schemataDefault bindings, IDefault bindings, IIDefault bindings, IIIDefault bindings, IVCustomizing the binding schemaPrimitive attributesConversions to Objects, IConversions to Objects, IICreating enumerationsContent modelsUsing JAXBThe EndJan 13, 2019JAXBJava Architecture for XML Binding2What is JAXB?JAXB is Java Architecture for XML BindingSAX and DOM are generic XML parsersThey will parse any well-structured XMLJAXB creates a parser that is specific to your DTDA JAXB parser will parse only valid XML (as defined by your DTD)DOM and JAXB both produce a tree in memoryDOM produces a generic tree; everything is a NodeJAXB produces a tree of Objects with names and attributes as described by your DTD3Advantages and disadvantagesAdvantages:JAXB requires a DTDUsing JAXB ensures the validity of your XMLA JAXB parser is actually faster than a generic SAX parserA tree created by JAXB is smaller than a DOM treeIt’s much easier to use a JAXB tree for application-specific codeYou can modify the tree and save it as XMLDisadvantages:JAXB requires a DTDHence, you cannot use JAXB to process generic XML (for example, if you are writing an XML editor or other tool)You must do additional work up front to tell JAXB what kind of tree you want it to constructBut this more than pays for itself by simplifying your applicationJAXB is new: Version 1.0 dates from Q4 (fourth quarter) 20024How JAXB worksJAXB takes as input two files: your DTD and a binding schema (which you also write)A binding schema is an XML document written in a “binding language” defined by JAXB (with extension .xjs)A binding schema is used to customize the JAXB outputYour binding schema can be very simple or quite complexJAXB produces as output Java source code which you compile and add to your programYour program will uses the specific classes generated by JAXBYour program can then read and write XML filesJAXB also provides an API for working directly with XMLSome examples in this lecture are taken from the JAXB User’s guide, http://java.sun.com/xml/jaxb/docs.html5A first exampleThe DTD: <!ELEMENT book (title, author, chapter+) ><!ELEMENT title (#PCDATA) ><!ELEMENT author (#PCDATA)><!ELEMENT chapter (#PCDATA) >The schema: <xml-java-binding-schema> <element name="book" type="class" root="true" /></xml-java-binding-schema>The results: public Book(); // constructorpublic String getTitle();public void setTitle(String x);public String getAuthor();public void setAuthor(String x);public List getChapter();public void deleteChapter();public void emptyChapter();Note 1: In these slides we only show the class outline, but JAXB creates a complete class for youNote 2: JAXB constructs names based on yours, with good capitalization style6Adding complexityAdding a choice can reduce the usefulness of the parser<!ELEMENT book (title, author, (prologue | preface), chapter+)><!ELEMENT prologue (#PCDATA) ><!ELEMENT preface (#PCDATA) >With the same binding schema, this gives:public Book();public List getContent();public void deleteContent();public void emptyContent();An improved binding schema can give better results7Improving the binding schema<xml-java-binding-schema> <element name="book" type="class" root="true"> <content> <element-ref name="title" /> <element-ref name="author” /> <choice property="prologue-or-preface" /> </content> </element></xml-java-binding-schema>Result is same as the original, plus methods for the choice:public Book(); // constructor. . .public void emptyChapter();public MarshallableObject getPrologueOrPreface();public void setPrologueOrPreface(MarshallableObject x);8Marshallingmarshal, v.t.: to place or arrange in ordermarshalling: the process of producing an XML document from Java objectsunmarshalling: the process of producing a content tree from an XML documentJAXB only allows you to unmarshal valid XML documentsJAXB only allows you to martial valid content trees into XML9Limitations of JAXBJAXB only supports DTDs and a subset of XML SchemasLater versions may support more schema languagesJAXB does not support the following legal DTD constructs:Internal subsetsNOTATIONsENTITY and ENTITIESEnumerated NOTATION types10A minimal binding schemaA JAXB binding schema is itself in XMLStart with: <xml-java-binding-schema version="1.0ea">The version is optional“ea” stands for “early access,” that is, not yet releasedPut in: <element name="rootName " type="class" root="true" />for each possible root elementAn XML document can have only one rootHowever, the DTD does not say what that root must beAny top-level element defined by the DTD may be a rootThe value of name must match exactly with the name in the DTDEnd with: </xml-java-binding-schema>11More complex schemataJAXB requires that you supply a binding schemaAs noted on the previous slide, this would be<xml-java-binding-schema version="1.0ea"> <element name="rootName" type="class" root="true" /></xml-java-binding-schema>With this binding schema, JAXB uses its default rule set to generate your “bindings”A binding is an association between an XML element and the Java code used to process that elementBy adding to this schema, you can customize the bindings and thus the generated Java code12Default bindings, IA “simple element” is one that has no attributes and only character contents:<!ELEMENT elementName (#PCDATA) >For simple elements, JAXB assumes: <element name="elementN ame" type="value"/>JAXB will treat this element as an instance variable of the class for its enclosing elementThis is the default binding, that is, this is what JAXB will assume unless you tell it otherwiseFor example, you could write this yourself, but set type="class"For simple elements, JAXB will generate these methods in the class of the enclosing element: void setElementName(String x); String getElementName();We will see later how to convert the #PCDATA into some type other than String13Default bindings, IIIf an element is not simple, JAXB will treat it as a classAttributes and simple subelements are treated as instance
View Full Document