advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 2 users have rated this item.
Email this articleEmail this article
 
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.

      Next Page: A Working JAXB Example


    Page 1: IntroductionPage 3: Using the Code Generator
    Page 2: A Working JAXB ExamplePage 4: Customization Through the Schema
    Untitled
    advertisement
    Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Intel PDF: Virtualization Delivers Data Center Efficiency
    Intel eBook: Managing the Evolving Data Center
    Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
    Symantec eBook: The Guide to E-Mail Archiving and Management
    Microsoft Article: RODCs Transform Branch Office Security
    Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
    Avaya Article: Advancing the State of the Art in Customer Service
    Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
    Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
    Go Parallel Article: Getting Started with TBB on Windows
    HP eBook: Storage Networking , Part 1
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Seminar: Efficiencies in Hardware/Software Virtualization
    HP Webcast: Disaster Recovery Planning
    Go Parallel Video: Performance and Threading Tools for Game Developers
    HP Video: StorageWorks EVA4400 and Oracle
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
    IBM Energy Efficiency eKIT: Learn How to Reduce Costs
    30-Day Trial: SPAMfighter Exchange Module
    Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    Microsoft Article: Silverlight Streaming--Free Video Hosting for All
    Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
    HP Demo: StorageWorks EVA4400
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES