Posted by & filed under Uncategorized.

This example has moved.

Remember the old days when you used to write a 150 line method which parsed your XML file into a Java class? Well that has long since been history. The next generation version of the JAXB XML processor, appropriately named JAXB2 has taken the liberty to make XML to Java conversion straightforward. Converting Java objects to XML has also been reduced to something trivial. Given an XSD, JAXB2’s XJC tool generates Java classes corresponding to your XSD Schema. Once you have the Java classes generated, reading and writing XML is a breeze.

The ONJava article by Deepak Vohra talks about this conversion process with JAXB. JAXB2 makes the process even easier as it generates fewer Java classes making the API calls even more intuitive. Marshalling and unmarshalling Java objects becomes dead simple.

The JAXB2 RI comes bundled with the Java Web Services Developers Pack 2.0

Given the following XSD, running the XJC command generates four Java files.

<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="catalog" type="catalogType"/>
 <xsd:complexType name="catalogType">
  <xsd:sequence>
   <xsd:element ref="journal"  minOccurs="0"
		maxOccurs="unbounded"/>
  </xsd:sequence>
  <xsd:attribute name="section" type="xsd:string"/>
  <xsd:attribute name="publisher" type="xsd:string"/>
 </xsd:complexType>
 <xsd:element name="journal" type="journalType"/>
 <xsd:complexType name="journalType">
  <xsd:sequence>
   <xsd:element ref="article"  minOccurs="0"
                       maxOccurs="unbounded"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:element name="article" type="articleType"/>
 <xsd:complexType name="articleType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="xsd:string"/>
  </xsd:sequence>
  <xsd:attribute name="level" type="xsd:string"/>
  <xsd:attribute name="date" type="xsd:string"/>
 </xsd:complexType>
</xsd:schema>

c:\Sunjwsdp-2.0jaxbbinxjc -p my.package schema.xsd

  • ArticleType.java
  • CatalogType.java
  • JournalType.java
  • ObjectFactory.java

ArticleType, CatalogType and JournalType correspond to the complex types defined in the schema. ObjectFactory is the object creation class which is used to create instances of the other objects. The one alteration that is needed is to add the @XmlRootElement annotation to the CatalogType class.

Here’s some sample code that uses this API to create a nice enough XML File and then reads it back to Java objects:

JAXBContext jaxbContext=JAXBContext.
                 newInstance("my.package");
Marshaller marshaller=jaxbContext.
				createMarshaller();
ObjectFactory factory=new ObjectFactory();
CatalogType catalog=(CatalogType)
	(factory.createCatalogType());
catalog.setSection("my catalog");
catalog.setPublisher("my publisher");

ArticleType articleType =
			factory.createArticleType();
articleType.setAuthor("an author");
articleType.setTitle("a nice article");
articleType.setLevel("rant");

JournalType journal =
		factory.createJournalType();
journal.getArticle().add(articleType);

catalog.getJournal().add(journal);
File file = new File("c:/jaxb2example.xml");
marshaller.
	setProperty("jaxb.formatted.output", true);
marshaller.marshal(catalog,
		new FileOutputStream(file));

Unmarshaller unmarshaller =
		jaxbContext.createUnmarshaller();
CatalogType c = (CatalogType)
	unmarshaller.unmarshal(file);

System.out.println("author is = " +
		c.getJournal().get(0).getArticle().
		get(0).getAuthor());

The XML file that gets generated and read conforms to the XSD:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<catalogType section="my catalog"
			publisher="my publisher">
  <journal>
    <article level="rant">
      <title>a nice article</title>
      <author>an author</author>
    </article>
  </journal>
</catalogType>

The compilation and runtime requires the following JAR’s in the classpath:

c:\Sun\jwsdp-2.0\jaxb\lib\jaxb-api.jar
c:\Sun\jwsdp-2.0\jaxb\lib\jaxb-impl.jar
c:\Sun\jwsdp-2.0\jaxb\lib\jaxb-xjc.jar

Damn easy.