Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Generate XML Mapping Code with JAXB

If you write any DOM or SAX code, you need to learn Java Architecture for XML Binding (JAXB). It rapidly generates XML mapping code for you, saving time and effort, and reducing both costs and risks.


advertisement
f you spend any time at all writing DOM or SAX code, then you need to know about the Java Architecture for XML Binding (JAXB). It rapidly generates XML mapping code for you, saving time and effort, and reducing both costs and risks. JAXB is a specification for a set of APIs and tools that generate java classes based on the domain model encoded in an XML schema. These classes then have the ability to read and write XML documents from files or streams.

JAXB is still in beta, but the powers that be are discussing a full release early this year. Of course, as with every new tool/technology, learning how to utilize it will take time. Fortunately, JAXB has been designed to be extremely easy to use, so the time investment should be minimal. In fact, for a large class of applications this simple article—a short, practical introduction to the JAXB technology—will show you enough about the tool to do the job.

Mapping/Binding/Translation
At the risk of stating the obvious, Java is an object-oriented language, while XML is hierarchically structured data. A number of strategies exist for dealing with this dichotomy:

  • Create an object model that describes the generalized structure of the data (as in DOM).
  • Deal with the data as pure data (as in SAX).
  • Encapsulate the data in domain objects, and let them manage it from there.


  • The latter solution is called mapping, binding, or translation, and it is the solution JAXB utilizes. In the mapping process, a developer usually uses a parser (SAX or DOM) to get at the data in an XML file and then uses the data to populate a domain object model. Often, the developer creates one class per element of the document, assigning values to attributes from either the data contained within the tag or the tag attributes. If a tag contains further elements, then the developer creates relationships from the class that represent the outer tag to the class representing the inner tag. Of course, before the developer populates an object with data, he or she has to first instantiate it. So the developer also writes code that manages both the creation and collection of objects.

    With SAX, this is usually done with a stack implementation. Every time the parser encounters a tag, the code decides whether it's an object or an attribute. If it's an object, the code creates a new instance of its representative class and pushes it onto the stack. If it's an attribute, the code passes it for processing to the object referenced by the head of the stack.

    In this approach, the code needs to have a detailed understanding of the document being parsed. It has to know that it should create a class when it encounters <address> and an attribute when it encounters <city>. This approach has obvious flexibility problems. If the schema changes, the code must be changed too. Also, although the pattern is generally applicable to mapping, the code is specific to a single schema. So it can't be reused.

    JAXB generates the mapping code based on the schema of the documents you'll be processing. It creates domain classes that model the schema and creates all the code you need to read and write documents.



    Comment and Contribute

     

     

     

     

     


    (Maximum characters: 1200). You have 1200 characters left.

     

     

    Sitemap