1 / 15

JAXB Overview

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>

rusti
Download Presentation

JAXB Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAXB Overview Vladimir Gapeyev

  2. 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

  3. Schema-based XML data binding compile Schema Classes instance of conforms unmarshal Objects Documents marshal JAXB Overview

  4. 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

  5. Mapping for atomic types Etc… JAXB Overview

  6. 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

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

  8. 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

  9. 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

  10. 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

  11. “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

  12. 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

  13. 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

  14. 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

  15. 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

More Related