Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Generate XML Mapping Code with JAXB : Page 3

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.

Using the Code Generator
Start by unpacking the xml and xsd files into a development directory, which contains a directory called 'src'. Create a new directory under src called model. On the command line issue the following instruction:

xjc –d src –p model play.xsd

If you've setup JAXB correctly you should see a message telling you that it's parsing the schema, followed by another message saying it's compiling the schema, and finally a list of the classes it has generated appears. Issuing the xjc command by itself produces a small help screen that explains the parameters. The options we've used here tell JAXB to both generate the classes into the src directory and put them in the model package.

If you have a legacy IDE like JBuilder, then you probably should generate javadocs for the classes you've just generated. If, like me, you use the impressive new version of IntelliJ IDEA, then this isn't an issue. IDEA reads the javadocs directly from the source and displays them in a tool-tip window at your request.

Next, open the model package in your favorite IDE and have a look at it. Its structure is very similar to the schema on which it's based. Where a one-to-one relationship exists, a method returns an object of the related type. For example, if you look at the Play class itself you'll see instance methods for retrieving an instance of Title.

Where a one-to-many relationship exists, a java.util.List class is returned. For example, instances of Play have a getAct() method that returns a List containing Act objects. The code I use to get the first act of the play (in ViewPanel's constructor) simply gets the first element of the list:

currentAct = (Act) play.getAct().get(0);

Of course, if I planned on outputting all of the acts in sequence I could just get an iterator from the list and use that. But I'm jumping ahead. The first thing you should do with your generated classes is use them to read an XML file. In the example, you can do this with the following three lines:

1 JAXBContext ctx = JAXBContext.newInstance("model"); 2 Unmarshaller u = ctx.createUnmarshaller(); 3 play = (Play) u.unmarshal(new File("/play/data/amsnd.xml"));

The argument on the line 1 is the name of the package to which the model belongs. The filename on line 3 is the XML file to parse; you should change this to refer to your copy of the file.

In this code, play is the head of the object tree that JAXB generates. From here I can use my domain model as I please. For example, the status bar contains details of the currently selected act and scene. Upon initialization, I use the following code to gather that information:

1 Act initialAct = (Act) Model.getPlay().getAct().get(0); 2 Scene initialScene = (Scene) initialAct.getScene().get(0);

The Model class is a singleton I've written to return the root of the domain object tree. Act and Scene are classes JAXB generates. Multiple objects, in this case acts and scenes, are stored in Lists. For the initial status bar display, I'm interested only in the first of each of these, so I can access the first element directly.

In our play schema, both act and scene have an attribute called title. JAXB translates XML attributes to object attributes. So, our Act and Scene classes have getTitle() methods that return String.

Look at the Line schema element. Notice it has no attributes, but its contents are the text for the line. Treat values like this as unnamed attributes, and access them with the getValue() method.

Building and Running
When you're building or running an application that uses JAXB, you'll need to include the following three libraries, which Sun distributes with JAXB:

  • jaxb-libs.jar
  • jaxb-api.jar
  • jaxb-ri.jar
  • You'll also need an XML parser. If you're using the 1.4.1 or later SDK, then you'll already have one. If not, Xerces from the Apache XML project (xml.apache.org) is a fairly useful and common one.

    Comment and Contribute






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



    Thanks for your registration, follow us on our social networks to keep up-to-date