Sorry it's been a while since my last post, I've been pretty busy so I wasn't able to update this blog at all. I wanted to show the easiest way I could find to convert an XML file to a Java object. There are many ways to do this however I wanted to find just a simple solution that wouldn't require me downloading extra jar files. Also I wanted to avoid a long complicated process to do this. I choose to use JAXB instead of something like XStream just for the fact that JAXB is included in Java 1.6, this way I could avoid having to download a third party jar (especially since I'm using ANT). Also I wanted to do this using an XSD (XML Schema) because this way the XML schema can be changed by someone who doesn't understand JAXB annotations or Java even. Ok so just to be clear I'm going to show the way I converted XML to Java using:

  • An XSD file
  • JAXB

1. First get your XSD file ready (example XSD from http://www.w3schools.com/schema/schema_example.asp)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="shiporder">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="orderperson" type="xs:string"/>
          <xs:element name="shipto">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
                <xs:element name="city" type="xs:string"/>
                <xs:element name="country" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="item" maxOccurs="unbounded">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="title" type="xs:string"/>
                <xs:element name="note" type="xs:string" minOccurs="0"/>
                <xs:element name="quantity" type="xs:positiveInteger"/>
                <xs:element name="price" type="xs:decimal"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
       </xs:sequence>
      <xs:attribute name="orderid" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

2. Run the xjc.exe/xjc.sh on the XSD file to generate the JAXB annotated class and object factory

xjc -p net.mytestname.helloworld shiporder.xsd

This xjc executable is located in the ${JAVA_HOME}/bin folder if your using JDK1.6 (remember it's only in the JDK). The -p option is the name of the package you wish the generated class to use. The last argument is the name of the XSD file to use.

3. After generating the java class file and the object factory java file, place these files into the appropriate folder in your java project (Whatever you defined your package name to be)

4. Once you have generated this java files it's actually very easy now with JAXB, just create a JAXBContext and then create a unmarshaller from the JAXBContext. Then just simply unmarshall the xml file to a java object.

JAXBContext context = JAXBContext.newInstance("net.mytestname.helloworld");
Unmarshaller unmarshaller = context.createUnmarshaller(); 
ShipOrder order = unmarshaller.unmarshal(new FileReader("C:\shiporder.xml"));

And that's all there is to it with JAXB. This was the easiest way I could find using an XSD file.