Docsity

Java Architecture for XML Binding (JAXB) - A Comprehensive Guide, Slides of Computer Science

Partial preview of the text

Download Java Architecture for XML Binding (JAXB) - A Comprehensive Guide and more Slides Computer Science in PDF only on Docsity!

JAXB

Java Architecture for XML Binding

What is JAXB?

• JAXB is Java Architecture for XML Binding

• SAX and DOM are generic XML parsers

  • They will parse any well-structured XML

• JAXB creates a parser that is specific to your DTD

  • A JAXB parser will parse only valid XML (as defined by

your DTD)

• DOM and JAXB both produce a tree in memory

  • DOM produces a generic tree; everything is a Node
  • JAXB produces a tree of Objects with names and

attributes as described by your DTD

How JAXB works

  • JAXB 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 output
  • Your binding schema can be very simple or quite complex
  • JAXB produces as output Java source code which you compile

and add to your program

  • Your program will uses the specific classes generated by JAXB
  • Your program can then read and write XML files
  • JAXB also provides an API for working directly with XML
  • Some examples in this lecture are taken from the JAXB User’s guide, http://java.sun.com/xml/jaxb/docs.html

A first example

  • The DTD:
  • The schema: <xml-java-binding-schema> </xml-java-binding-schema>
  • The results: public Book(); // constructor public 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 you Note 2: JAXB constructs names based on yours, with good capitalization style

Improving the binding schema

  • <xml-java-binding-schema> <element-ref name="title" /> <element-ref name="author” /> </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);

Marshalling

• marshal, v.t.: to place or arrange in order

• marshalling: the process of producing an XML

document from Java objects

• unmarshalling: the process of producing a

content tree from an XML document

• JAXB only allows you to unmarshal valid XML

documents

• JAXB only allows you to martial valid content

trees into XML

A minimal binding schema

  • A JAXB binding schema is itself in XML
  • Start with: <xml-java-binding-schema version="1.0ea">
    • The version is optional
    • “ea” stands for “early access,” that is, not yet released
  • Put in:

for each possible root element

  • An XML document can have only one root
  • However, the DTD does not say what that root must be
  • Any top-level element defined by the DTD may be a root
  • The value of name must match exactly with the name in the DTD
  • End with: </xml-java-binding-schema>

More complex schemata

• JAXB requires that you supply a binding

schema

• As noted on the previous slide, this would be

<xml-java-binding-schema version="1.0ea">

</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

element

Default bindings, II

  • If an element is not simple, JAXB will treat it as a class
  • Attributes and simple subelements are treated as instance variables
  • DTD:
  • Binding: <element-ref name=" subElement1 " /> <element-ref name=" subElement2 " />
  • Java: class ElementName extends MarshallableObject {

void set AttributeName1 (String x); String get AttributeName1 (); String get SubElement1 (); void set SubElement1 (String x); // Non-simple subElement2 is described on the next slide

Default bindings, III

  • If an element contains a subelement that is defined by a

class, the code generated will be different

  • <element-ref name=" subElement2 " />
  • Results in: class ElementName extends MarshallableObject { SubElement2 get SubElement2 (); void set SubElement2 ( SubElement2 x); ...}
  • Elsewhere, the DTD definition for subElement2 will result in: class SubElement2 extends MarshallableObject { ... }

Customizing the binding schema

  • You won’t actually see these default bindings anywhere--

they are just assumed

  • If a default binding is OK with you, don’t do anything
  • If you don’t like a default binding, just write your own
  • Here’s the minimal binding you must write: <xml-java-binding-schema> </xml-java-binding-schema>
  • Start by “opening up” the root element:

<xml-java-binding-schema> </xml-java-binding-schema>

  • Now you have somewhere to put your customizations

Primitive attributes

  • By default, attributes are assumed to be Strings
    • class SomeElement extends MarshallableObject { void set SomeAttribute (String x); String get SomeAttribute ();
  • You can define your own binding and use the convert

attribute to force the defined attribute to be a primitive, such

as an int:

  • class SomeElement extends MarshallableObject { void set SomeAttribute (int x); int get SomeAttribute ();

Conversions to Objects, II

• There is a constructor for Date that takes a

String as its one argument, but this

constructor is deprecated

– This is because there are many ways to write dates

– For an object like this, you need to supply

methods to “parse” and “print”

• <conversion name="MyDate" type="java.util.Date”

parse="MyDate.parseDate" print="MyDate.printDate"/>

• Your class, MyDate, would extend Date and

provide parseDate and printDate methods

Creating enumerations

  • - defines an attribute of shirt that can take on one of a predefined set of values - A typesafe enum is a class whose instances are a predefined set of values - To create a typesafe enum for size: - - - You get: - public final class ShirtSize { public final static ShirtSize SMALL; public final static ShirtSize MEDIUM; public final static ShirtSize LARGE; public static ShirtSize parse(String x); public String toString(); }