In this article, Martin Bond discusses XML and its associated APIs and standards, and how XML can be used to create flexible structured data that is inherently portable. This excerpt is from chapter (Day) 16 of Teach Yourself J2EE in 21 Days, second edition, by Martin Bond, et. al. (Sams, ISBN: 0672325586)
DOM is a useful API allowing you to build and transform XML documents in memory. Unfortunately, DOM is somewhat slow and resource hungry. To address these problems, the Java Architecture for XML Binding (JAXB) has been developed through the Java Community Process (JCP) with an expert group consisting of representatives from many commercial organizations.
JAXB provides a mechanism that simplifies the creation and maintenance of XML-enabled Java applications. It does this by using an XML schema compiler (only DTDs and a subset of XML schemas and namespaces at the time of this writing) that translates XML DTDs into one or more Java classes, thereby removing the burden from the developer to write complex parsing code.
The generated classes handle all the details of XML parsing and formatting, including code to perform error and validity checking of incoming and outgoing XML documents, which ensures that only valid, error-free XML is accepted.
Because the code has been generated for a specific schema, the generated classes are more efficient than those in a generic SAX or DOM parser. Most important, a JAXB parser often requires a much smaller footprint in memory than a generic parser.
Classes created with JAXB do not include tree-manipulation capability, which is one factor that contributes to the small memory footprint of a JAXB object tree. If you want to build an object representation of XML data, but need to get around the memory limitations of DOM, you should use JAXB.
These following two bulleted lists summarize the advantages of JAXB and JAXP so you can decide which one is right for your application.
Use JAXB when you want to
Access data in memory, but do not need tree manipulation capabilities
Process only data that is valid
Convert data to different types
Generate classes based on a DTD or XML schema
Build object representations of XML data
Use JAXP when you want to
Have flexibility with regard to the way you access the data, either serially with SAX or randomly in memory with DOM
Use your same processing code with documents based on different DTDs
Parse documents that are not necessarily valid
Apply XSLT transformations
Insert or remove components from an in-memory XML tree
This chapter is from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond et. al. (Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.