Title: JAXB Overview
1JAXB Overview
2XML data binding
Java
XML
ObjectFactory f new ObjectFactory() Book
b f.createBook() b.setTitle(Compilers) List
a b.getAuthor() a.add(Aho) a.add(Sethi)
a.add(Ullman) b.setPages(796)
ltbookgt lttitlegtCompilerslt/titlegt
ltauthorgtAholt/authorgt ltauthorgtSethilt/authorgt
ltauthorgtUllmanlt/authorgt ltpagesgt796lt/pagesgt lt/boo
kgt
3Schema-based XML data binding
compile
Schema
Classes
instance of
conforms
unmarshal
Objects
Documents
marshal
4JAXB 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
5Mapping for atomic types
xsstring ? java.lang.string
xsdecimal ? java.math.BigDecimal
xsint ? int
xsQName ? javax.xml.namespace.QName
xsdate ? java.util.Calendar
Etc
6Elements with simple content
interface Zip extends javax.xml.bind.Element
int getValue() void setValue(int)
ltxselement namezip typexsint /gt
7Elements with sequence and repetition
ltxselement namebook gt ltxsComplexTypegt
ltxssequencegt ltxselement nametitle
typexsstring/gt ltxselement nameauthor
typexsstring maxOccursunbounded/gt
ltxselement namepages typexsint/gt
lt/xssequencegt lt/xsComplexTypegt lt/xselementgt
interface Book extends BookType,
javax.xml.bind.Element interface BookType
String getTitile() void setTitle(String x)
List getAuthor() int getPages() void
setPages(int x)
8Elements with choice
ltxscomplexType namevolumegt
ltxschoicegt ltxselement
refbook/gt ltxselement
refjournal/gt lt/xschoicegt lt/xscomplexTypegt
interface Volume BookType getBook() void
setBook(BookType) JournalType getJournal()
void setJournal(JournalType)
9Customization isSet() method
ltxsannotationgtltxsappinfogt ltjxbglobalBindings
generateIsSetMethodtrue"/gt lt/xsappinfogtlt/xsan
notationgt
ltxscomplexType namevolumegt
ltxschoicegt ltxselement
refbook/gt ltxselement
refjournal/gt lt/xschoicegt lt/xscomplexTypegt
interface Volume BookType getBook() void
setBook(BookType) boolean isSetBook()
JournalType getJournal() void
setJournal(JournalType) boolean
isSetJournal()
10No roundtripping
interface T getA() setA() getB()
setB()
type t (element a, element b) (element
b, element a)
Type t members lta/gtltb/gt and ltb/gtlta/gt
unmarshal marshal / identity unmarshal
marshal unmarshal unmarshal
11General content style
ltxscomplexType nametgt ltxschoice
maxOccursunboundedgt ltelement refbook/gt
ltelement refjournal/gt lt/xschoicegt lt/xscomple
xTypegt
interface T List getBookOrJournal()
- Plus a constraint
- The only list members are
- Book objects
- Journal objects
12Model groups, by default
ltxsgroup name"bookName"gt ltxssequencegt
ltxselement name"title" type"xsstring"/gt
ltxselement name"author"
type"xsstring/gt lt/xssequencegt lt/xsgroupgt ltx
scomplexType name"bookType"gt ltxssequencegt
ltxsgroup ref"bookName"/gt ltxselement
name"pages" type"xsint"/gt
lt/xssequencegt lt/xscomplexTypegt
interface BookType String getTitle() void
setTitle(String x) String getAuthor() void
setAuthor(String) int getPages() void
setPages(int x)
13Model groups, customized
ltxsgroup name"bookName"gt ltxssequencegt
ltxselement name"title" type"xsstring"/gt
ltxselement name"author"
type"xsstring/gt lt/xssequencegt lt/xsgroupgt ltx
scomplexType name"bookType"gt ltxssequencegt
ltxsgroup ref"bookName"/gt ltxselement
name"pages" type"xsint"/gt
lt/xssequencegt lt/xscomplexTypegt
interface BookName String getTitle() void
setTitle(String x) String getAuthor() void
setAuthor(String) interface BookType
BookName getBookName() void setBookName(BookNam
e) int getPages() void setPages(int x)
14Summary 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
15Opportunities
- 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)