DOC PREVIEW
Penn CIT 597 - Java Architecture for XML Binding

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JAXB Java Architecture for XML Binding Jan 13 2019 What is JAXB JAXB is Java Architecture for XML Binding SAX and DOM are generic XML parsers JAXB creates a parser that is specific to your DTD They will parse any well structured XML 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 2 Advantages and disadvantages Advantages JAXB requires a DTD Using JAXB ensures the validity of your XML A JAXB parser is actually faster than a generic SAX parser A tree created by JAXB is smaller than a DOM tree It s much easier to use a JAXB tree for application specific code You can modify the tree and save it as XML Disadvantages JAXB requires a DTD You must do additional work up front to tell JAXB what kind of tree you want it to construct Hence you cannot use JAXB to process generic XML for example if you are writing an XML editor or other tool But this more than pays for itself by simplifying your application JAXB is new Version 1 0 dates from Q4 fourth quarter 2002 3 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 4 A first example The DTD ELEMENT book title author chapter ELEMENT title PCDATA ELEMENT author PCDATA ELEMENT chapter PCDATA The schema xml java binding schema element name book type class root true xml java binding schema The results public public public public public public public public Book constructor String getTitle void setTitle String x String getAuthor void setAuthor String x List getChapter void deleteChapter 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 5 Adding complexity Adding a choice can reduce the usefulness of the parser ELEMENT book title author prologue preface chapter ELEMENT prologue PCDATA ELEMENT preface PCDATA With the same binding schema this gives public Book public List getContent public void deleteContent public void emptyContent An improved binding schema can give better results 6 Improving the binding schema xml java binding schema element name book type class root true content element ref name title element ref name author choice property prologue or preface content element xml java binding schema Result is same as the original plus methods for the choice public public public public Book constructor void emptyChapter MarshallableObject getPrologueOrPreface void setPrologueOrPreface MarshallableObject x 7 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 8 Limitations of JAXB JAXB only supports DTDs and a subset of XML Schemas Later versions may support more schema languages JAXB does not support the following legal DTD constructs Internal subsets NOTATIONs ENTITY and ENTITIES Enumerated NOTATION types 9 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 element name rootName type class root true 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 10 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 element name rootName type class root true 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 By adding to this schema you can customize the bindings and thus the generated Java code 11 Default bindings I A simple element is one that has no attributes and only character contents ELEMENT elementName PCDATA For simple elements JAXB assumes element name elementName type value JAXB will treat this element as an instance variable of the class for its enclosing element This is the default binding that is this is what JAXB will assume unless you tell it otherwise For example you could write this yourself but set type class For simple elements JAXB will generate these methods in the class of the enclosing element void setElementName String x String getElementName We will see later how to convert the PCDATA into some type other than String 12 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 ELEMENT elementName subElement1 subElement2 ATTLIST elementName attributeName CDATA IMPLIED Binding element name elementName type class attribute name attributeName content element ref name subElement1 simple element element ref name subElement2 complex element content element Java class ElementName extends MarshallableObject void setAttributeName1 String x String getAttributeName1 String getSubElement1 void setSubElement1 String x Non simple subElement2 is described on the next slide 13 Default bindings III If an element contains a subelement that is defined by a class the code generated will be different element name elementName type class content element ref name subElement2 Note that element ref means this is a reference to an element that is defined elsewhere not the element itself content element Results in class ElementName extends MarshallableObject SubElement2 getSubElement2


View Full Document

Penn CIT 597 - Java Architecture for XML Binding

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Java Architecture for XML Binding
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Architecture for XML Binding and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Architecture for XML Binding and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?