150 likes | 287 Vues
JAXB (Java Architecture for XML Binding) allows Java developers to bind XML data to Java objects. This overview covers creating and marshalling XML objects, such as books with titles and authors, using an ObjectFactory. It explores schema-based binding, customization techniques, and the handling of complex types through interfaces. Topics include default bindings, Java naming conventions, schema atomic types, and structured types. The content highlights challenges with mapping and provides insights into formal specifications and dynamic constraints, enhancing your understanding of XML data handling in Java applications.
E N D
JAXB Overview Vladimir Gapeyev
XML 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); <book> <title>Compilers</title> <author>Aho</author> <author>Sethi</author> <author>Ullman</author> <pages>796</pages> </book> JAXB Overview
Schema-based XML data binding compile Schema Classes instance of conforms unmarshal Objects Documents marshal JAXB Overview
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 JAXB Overview
Mapping for atomic types Etc… JAXB Overview
Elements with simple content interface Zip extends javax.xml.bind.Element { int getValue(); void setValue(int); } <xs:element name=“zip” type=“xs:int” /> JAXB Overview
Elements with sequence and repetition <xs:element name=“book” > <xs:ComplexType> <xs:sequence> <xs:element name=“title” type=“xs:string”/> <xs:element name=“author” type=“xs:string” maxOccurs=“unbounded”/> <xs:element name=“pages” type=“xs:int”/> </xs:sequence> </xs:ComplexType> </xs:element> interface Book extends BookType, javax.xml.bind.Element {} interface BookType { String getTitile(); void setTitle(String x); List getAuthor(); int getPages(); void setPages(int x); } JAXB Overview
Elements with choice <xs:complexType name=“volume”> <xs:choice> <xs:element ref=“book”/> <xs:element ref=“journal”/> </xs:choice> </xs:complexType> interface Volume { BookType getBook(); void setBook(BookType); JournalType getJournal(); void setJournal(JournalType); } JAXB Overview
Customization: isSet() method <xs:annotation><xs:appinfo> <jxb:globalBindings generateIsSetMethod=“true"/> </xs:appinfo></xs:annotation> <xs:complexType name=“volume”> <xs:choice> <xs:element ref=“book”/> <xs:element ref=“journal”/> </xs:choice> </xs:complexType> interface Volume { BookType getBook(); void setBook(BookType); boolean isSetBook(); JournalType getJournal(); void setJournal(JournalType); boolean isSetJournal(); } JAXB Overview
No roundtripping interface T { getA(); setA(); getB(); setB(); } type t { (element a, element b) | (element b, element a) } Type t members:<a/><b/>and<b/><a/> unmarshal; marshal =/= identity unmarshal; marshal; unmarshal = unmarshal JAXB Overview
“General content style” <xs:complexType name=“t”> <xs:choice maxOccurs=“unbounded”> <element ref=“book”/> <element ref=“journal”/> </xs:choice> </xs:complexType> interface T { List getBookOrJournal() } • Plus a constraint: • The only list members are • Book objects • Journal objects JAXB Overview
Model groups, by default <xs:group name="bookName"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string“/> </xs:sequence> </xs:group> <xs:complexType name="bookType"> <xs:sequence> <xs:group ref="bookName"/> <xs:element name="pages" type="xs:int"/> </xs:sequence> </xs:complexType> interface BookType { String getTitle(); void setTitle(String x); String getAuthor(); void setAuthor(String); int getPages(); void setPages(int x); } JAXB Overview
Model groups, customized <xs:group name="bookName"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string“/> </xs:sequence> </xs:group> <xs:complexType name="bookType"> <xs:sequence> <xs:group ref="bookName"/> <xs:element name="pages" type="xs:int"/> </xs:sequence> </xs:complexType> interface BookName { String getTitle(); void setTitle(String x); String getAuthor(); void setAuthor(String); } interface BookType { BookName getBookName(); void setBookName(BookName); int getPages(); void setPages(int x); } JAXB Overview
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 JAXB Overview
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) JAXB Overview