Tuesday, June 06, 2006

JAXP and JAXB

6/6/6 The number of the beast -- Get to know JAXB.

The introduction
Java Architecture for XML binding (Article, Sun March 2003)

My short version of this article

JAXP - Java API for XML Processing
  • Provides SAX and DOM
    • Used to scan XML documents and break it up to pieces that are made available to the application.
  • Differences Between SAX and DOM
    • SAX starts from the beginning of the document and passes each piece to the application as it finds it. Nothing is stored in memory.
    • DOM creates a tree of objects in memory that hte application can navigate through and access and manipulate data.
    • Difference - SAX cannot update data to the XML file but in DOM we can.
JAXB - Java Architecture for XML binding
  • Another Java API that can make it easier to access XML documents
  • Check out the example (Accessing an XML Document, JAXP and JAXB) or just be happy with the fact that: "Using JAXB instead of JAXP, there is no need to create and use a parser using and no need to write a content handler with callback methods. What this means is that developers can access and process XML data without having to know XML or XML processing".
  • Using JAXB, you would:
    • Unmarshal the document into Java content objects. The Java content objects represent the content and organization of the XML document, and are directly available to your program.
  • What is a schema?
    • A schema is an XML specification that governs the allowable components of an XML document and the relationships between the components.
  • JAXB requires that the XML document you want to access has a schema, and that schema is written in the W3C XML Schema Language
  • Binding a schema means generating (using a binding compiler) a set of Java classes that represents the schema. The binding compiler generates a set of interfaces and a set of classes that implement the interfaces. (Eg: books.xsd would generate BookType.java, BookTypeImpl.java etc.)
  • Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document. The content objects are instances of the classes produced by the binding compiler. See example (how to unmarshal the XML document)

No comments: