XML in Java: Data binding, part 1th: Code generation methods-JAXB and others

Source: Internet
Author: User
Tags object serialization regular expression requires

Data binding provides a simple and straightforward way to use XML in a Java platform application. With data binding, applications can largely ignore the actual structure of XML documents and directly use the data content of those documents. Although this approach is not suitable for all applications, it is generally ideal for applications that use XML for data exchange.

In addition to simplifying programming, data binding provides some other benefits. Because data binding abstracts many document details, it typically requires less memory than document model methods, such as DOM or JDOM, for processing documents in memory. You will also find that the data-binding method accesses data within the program faster than the document model method because you do not need to traverse the document structure to get the data. Finally, when entering, some special types of data, such as numbers and dates, can be converted to internal representations instead of text; This enables applications to use data values more efficiently.

You might want to know when to use the document model method if there are so many benefits to data binding. Document model methods are basically required in the following two main scenarios:

When the application is really concerned with the details of the document structure. For example, if you are writing an XML document editor, you will want to adopt a document model instead of using data binding.

When the document being processed does not need to follow a fixed structure. For example, data binding is not a good way to implement a regular XML document database.

Back to Binding

Last year, I wrote an article about how to use the Castor framework to map data binding for Java objects to XML documents. I have promised to write a follow-up article that will explore code generation methods, including an introduction to Jaxb,java Community Process (JCP) is developing JAXB, a standard API written in the Java language for data binding. Shortly after the publication of the earlier article, Sun announced significant adjustments to the direction of JAXB. Because of this change, so I think, in order to get closer to the final JAXB code, it is best not to write this follow-up article, now, I am very happy, and finally can write this article!

Data Binding Dictionary

Here is a miniature dictionary that contains some of the terms I used in this article:

Grammar (grammar) is a set of rules used to define the structure of a series of XML documents. One class of grammars is the document type definition (DEFINITION,DTD) format defined by the XML specification. Another class of increasingly popular grammars is the Universal XML Schema (schema) format defined by the XML Schema specification. The grammar defines which elements and attributes can appear in the document, and how the elements are nested within the document (usually including the order and number of nested elements). Some types of grammars, such as schemas, can go further and match the content of character data to a particular data type or even regular expression. In this article, I often use the term description as an informal way to refer to the grammar of a series of documents.

Marshalling (marshalling) is the process of generating an XML representation of an object in memory. As with Java object serialization, this representation needs to include all dependent objects: objects referenced by our main object, objects referenced by those objects, and so on.

Data decomposition (unmarshalling) is an inverse process that constructs an object (and possibly a graph of linked objects) in memory based on the XML representation.

In this article, I'll discuss five XML data binding frameworks that generate Java language code based on XML document grammars: JAXB, Castor, JBind, Quick, and Zeus. All of them are available for free, and in addition to JAXB, four other frameworks can be used in open source and patent projects. The license for the current JAXB reference implementation Beta is only allowed for evaluation, but this situation is likely to change when it is released as a product. JAXB, Castor, and JBind both provide code generation based on Schema descriptions of XML documents, and Quick and Zeus generate code based on the DTD description. Castor and Quick also support the mapping of existing classes to XML as another way to generate code.

Each of these frameworks has pros and cons, so I'll try to point out the best (and worst) features of each framework. In the 2nd part, I'll show you further how these frameworks work on some sample documents, and also explore how the existing data-binding framework lacks some important features for many types of applications.

Compared to the mapping bindings method I described in previous articles, generating Java language code based on Schema or DTD grammars has some outstanding advantages. With the generated code, you can determine that the data object is correctly linked to the XML document, unlike the mapping binding method, which requires direct assignment of links and ensures that all structural variants are covered correctly. When using schemas, you can even use the type information provided by the grammar to generate code with appropriate data types.

Code generation methods also have some drawbacks. This approach causes the application data structure to be tightly coupled to the XML document structure. In addition, it may qualify you to use simple data classes (passive data containers without associated behavior), rather than real object classes, and may limit the flexibility of custom transformations to apply data during marshalling and data decomposition. Later in this article, I'll weigh both the code generation and the mapping bindings.

Data and Code

For the performance tests that will be discussed in part 2nd, I generate the code with each data-binding framework. The documentation for the performance test contains information about the simulated flight timetable. Here is a sample document that you can feel the structure of:

Listing 1. Sample Document

<?xml version= "1.0"?>
<timetable>
<carrier ident= "AR" >
<rating>9</rating>
<URL>http://www.arcticairlines.com</URL>
<name>arctic airlines</name>
</carrier>
<carrier ident= "CA" >
<rating>7</rating>
<URL>http://www.combinedlines.com</URL>
<name>combined airlines</name>
</carrier>
<airport ident= "Sea" >
<location>seattle, wa</location>
<name>seattle-tacoma International airport</name>
</airport>
<airport ident= "LAX" >
<location>los Angeles, ca</location>
<name>los Angeles International airport</name>
</airport>
<route from= "Sea" to= "LAX" >
<flight carrier= "AR" >
<number>426</number>
<depart>6:23a</depart>
<arrive>8:42a</arrive>
</flight>
<flight carrier= "CA" >
<number>833</number>
<depart>8:10a</depart>
<arrive>10:52a</arrive>
</flight>
<flight carrier= "AR" >
<number>433</number>
<depart>9:00a</depart>
<arrive>11:36a</arrive>
</flight>
</route>
<route from= "LAX" to= "Sea" >
<flight carrier= "CA" >
<number>311</number>
<depart>7:45a</depart>
<arrive>10:20a</arrive>
</flight>
<flight carrier= "AR" >
<number>593</number>
<depart>9:27a</depart>
<arrive>12:04p</arrive>
</flight>
<flight carrier= "AR" >
<number>102</number>
<depart>12:30p</depart>
<arrive>3:07p</arrive>
</flight>
</route>
</timetable>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.