Вы находитесь на странице: 1из 15

JAXB Overview

Vladimir Gapeyev
XML data binding
XML: Java:

ObjectFactory f =
<book>
new ObjectFactory();
<title>Compilers</title>
Book b = f.createBook();
<author>Aho</author>
b.setTitle(“Compilers”);
<author>Sethi</author>
List a = b.getAuthor();
<author>Ullman</author>
a.add(“Aho”);
<pages>796</pages>
a.add(“Sethi”);
</book>
a.add(“Ullman”);
b.setPages(796);

10/15/08 JAXB Overview 2


Schema-based XML data
binding
compile
Schema Classes

conforms instance of

unmarshal
Documents Objects
marshal

10/15/08 JAXB Overview 3


JAXB: Java Architecture for XML
Binding
Specification:
 Subset of XML Schema
 Default bindings
 XML names  Java names
 Schema atomic types  predefined Java types
 Schema structured types  Java interfaces
 javax.xml.bind – framework interfaces
 Language for binding customization

Reference implementation:
 Schema-to-Java compliler

 Generates classes as well

10/15/08 JAXB Overview 4


Mapping for atomic types

xs:string  java.lang.string

xs:decimal  java.math.BigDecimal

xs:int  int

xs:QName  javax.xml.namespace.QName

xs:date  java.util.Calendar

Etc…

10/15/08 JAXB Overview 5


Elements with simple content

interface Zip extends


javax.xml.bind.Element
<xs:element name=“zip” {
type=“xs:int” /> int getValue();
void setValue(int);
}

10/15/08 JAXB Overview 6


Elements with sequence and
repetition
<xs:element name=“book” > interface Book extends
<xs:ComplexType> BookType,
<xs:sequence> javax.xml.bind.Element {}
<xs:element name=“title”
type=“xs:string”/> interface BookType {
<xs:element name=“author” String getTitile();
type=“xs:string” void setTitle(String x);
maxOccurs=“unbounded”/> List getAuthor();
<xs:element name=“pages” int getPages();
type=“xs:int”/> void setPages(int x);
</xs:sequence> }
</xs:ComplexType>
</xs:element>

10/15/08 JAXB Overview 7


Elements with choice
<xs:complexType interface Volume {
name=“volume”> BookType getBook();
<xs:choice> void setBook(BookType);
<xs:element
ref=“book”/> JournalType getJournal();
<xs:element void setJournal(JournalType);
ref=“journal”/> }
</xs:choice>
</xs:complexType>

10/15/08 JAXB Overview 8


Customization: isSet()
method
<xs:annotation><xs:appinfo>
<jxb:globalBindings generateIsSetMethod=“true"/>
</xs:appinfo></xs:annotation>

<xs:complexType interface Volume {


name=“volume”> BookType getBook();
<xs:choice> void setBook(BookType);
<xs:element boolean isSetBook();
ref=“book”/>
<xs:element JournalType getJournal();
ref=“journal”/> void setJournal(JournalType);
</xs:choice> boolean isSetJournal();
</xs:complexType> }

10/15/08 JAXB Overview 9


No roundtripping
type t { interface T {
(element a, element b) getA(); setA();
| getB(); setB();
(element b, element a) }
}

Type t members: <a/><b/> and <b/><a/>

unmarshal; marshal =/= identity

unmarshal; marshal; unmarshal = unmarshal

10/15/08 JAXB Overview 10


“General content style”
<xs:complexType name=“t”> interface T {
<xs:choice List getBookOrJournal()
maxOccurs=“unbounded”> }
<element ref=“book”/>
<element ref=“journal”/>
</xs:choice> Plus a constraint:
</xs:complexType> The only list members are
• Book objects
• Journal objects

10/15/08 JAXB Overview 11


Model groups, by default
<xs:group name="bookName">
<xs:sequence> interface BookType {
<xs:element name="title" String getTitle();
type="xs:string"/> void setTitle(String x);
<xs:element name="author" String getAuthor();
type="xs:string“/> void setAuthor(String);
</xs:sequence> int getPages();
</xs:group> void setPages(int x);
}
<xs:complexType name="bookType">
<xs:sequence>
<xs:group ref="bookName"/>
<xs:element name="pages"
type="xs:int"/>
</xs:sequence>
</xs:complexType>

10/15/08 JAXB Overview 12


Model groups, customized
<xs:group name="bookName">
interface BookName {
<xs:sequence>
String getTitle();
<xs:element name="title"
void setTitle(String x);
type="xs:string"/>
String getAuthor();
<xs:element name="author"
void setAuthor(String);
type="xs:string“/>
}
</xs:sequence>
</xs:group>
interface BookType {
BookName getBookName();
<xs:complexType name="bookType">
void setBookName(BookName);
<xs:sequence>
int getPages();
<xs:group ref="bookName"/>
void setPages(int x);
<xs:element name="pages"
}
type="xs:int"/>
</xs:sequence>
</xs:complexType>

10/15/08 JAXB Overview 13


Summary of observations

 Mapping often loses static typing information


– need to employ dynamic constraints
 Complex content models with choice are
most problematic
 Alternative mapping styles
 No good declarative specifications for the
mappings

10/15/08 JAXB Overview 14


Opportunities

 Formal specification of mapping styles


 Formulate desired mapping properties
 Verify that the mapping styles enjoy the
properties
 A toolkit for generating mapping compilers
(as opposed to a single compiler)

10/15/08 JAXB Overview 15

Вам также может понравиться