esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I use JAXB?
JAXB allows for a mapping between Java objects and XML files. The following example goes through a couple steps so you get a feel of this powerful library.

Download the early-access implementation from http://java.sun.com/xml/jaxb, and add the JAR files to your classpath.

First, write a DTD that describes your object.
customer.dtd (!!remove the space between the ? and xml) :

This code sample is only viewable to esus.com members
Login or become a member!


To map this description of a customer to an object (tree), you can invoke the schema to java compiler:

 
This code sample is only viewable to esus.com members
Login or become a member!


output:

 
This code sample is only viewable to esus.com members
Login or become a member!


Two files have been created that are the equivalent of the DTD. You can further customize the mapping by creating a .xjs file. For example, in the following customer.xjs file, it is specified that the attribute custid is mapped onto an int in the Java object (the default was a String). Also, the classes are to be placed in the package
com.esus.jaxbtest.
customer.xjs (!!remove the space between ? and xml):

This code sample is only viewable to esus.com members
Login or become a member!


Now run the conversion utility again, but specify your customized mapping:

 
This code sample is only viewable to esus.com members
Login or become a member!


output is now:

 
This code sample is only viewable to esus.com members
Login or become a member!


Now, let's test the mapping and create an XML file data.xml that describes one customer.

data.xml (!!remove the space between ? and xml):

This code sample is only viewable to esus.com members
Login or become a member!


java Main:

This code sample is only viewable to esus.com members
Login or become a member!


Notice that the conversion halts because data.xml does not contain the tag addline2 while it was required in the original DTD customer.dtd. Let's make addline2 optional, and regenerate.

customer.dtd (!!remove the space between ? and xml):

This code sample is only viewable to esus.com members
Login or become a member!





 
This code sample is only viewable to esus.com members
Login or become a member!


Notice now that the unmarshalling fails because our customer record does not contain the required custid attribute. Add it!
data.xml (!!remove the space between ? and xml):

This code sample is only viewable to esus.com members
Login or become a member!


Now run again:

 
This code sample is only viewable to esus.com members
Login or become a member!


To go the other way:
Main.java:

This code sample is only viewable to esus.com members
Login or become a member!


After executing this program, the following file out.xml will be created:

 
This code sample is only viewable to esus.com members
Login or become a member!




Further Information
Author of answer: Joris Van den Bogaert

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.