A while back a friend of mine asked for a link on how to use JAXB. So I went to Google and attempted to find one. After a few minutes of searching I said, “Wait a minute, it would take me like five minutes to work up a sample.” Enjoy.
JAXB is a technology to read and write XML documents, among other things. The following tutorial uses the annotations provided by JAXB, therefore requiring Java 5 or greater. Also, an important thing to note is that Java 6 has JAXB in the JRE distribution! Java 5 developers just need to download the jars.
So lets get started. Lets assume you have (or have created) the following XML document:
<?xml version="1.0" encoding="UTF-8"?> <car> <feature name="leather"> <desc>Corinthian</desc> </feature> <feature name="automatic"> <desc>5 speed</desc> </feature> </car>
The first thing to do is to create the classes necessary to represent the XML document. In this case we will create two classes; one for each element (car and feature).
Car class:
// the tag type this class represents // and the variable order (not necessary) @XmlType(name = "car", propOrder = {"color", "features"}) // the tag name in the xml document @XmlRootElement(name = "car") public class JaxbCar { // any simple type, String, primitives @XmlElement(required = false) // the variable, uses "color" as the name to // look for in the xml document private String color; // list of stuff, the name in the xml document @XmlElement(required = false, name = "feature") // the variable private List<JaxbFeature> features = new arrayList<JaxbFeature>(); // getter/setters }
Feature class:
@XmlType(name = "feature", propOrder = {"name", "description"}) @XmlRootElement(name = "feature") public class JaxbFeature { // defines an attribute instead of an element @XmlAttribute(required = false) private String name; @XmlElement(required = false, name = "desc") private String description; // getter/setters }
Next, we need to create an XML registry, which will be used by the Unmarshaller to create the classes when reading the XML document.
ObjectFactory class:
@XmlRegistry public class ObjectFactory { // there has to be a method for each // element in the xml document public JaxbCar createCar() { return new JaxbCar(); } public JaxbFeature createFeature() { return new JaxbFeature(); } }
I can’t remember if the name of the registry has to be ObjectFactory. I don’t think it does (in which case you have to configure it somewhere), but I haven’t had time to research it.
Next we have the definition of the schema which is done on the package-info.java file in the same package as the above classes.
package-info.java file:
@javax.xml.bind.annotation.XmlSchema(namespace="", elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED) @javax.xml.bind.annotation.XmlAccessorType(value=javax.xml.bind.annotation.XmlAccessType.NONE) package thep.ackage.containing.the.above.classes;
Once the above is created we can unmarshall (read) and marshall (write) our XML document.
Sample method:
// create the Jaxb context JAXBContext context = JAXBContext.newInstance("thep.ackage.containing.the.above.classes"); // create an unmarshaller Unmarshaller unmarshaller = context.createUnmarshaller(); // use a marshaller to write xml try { // use the unmarshaller to translate an inputstream JaxbCar car = (JaxbCar) unmarshaller.unmarshal(inputstream); } catch ( MalformedURLException e ) { } catch ( ProtocolException e ) { } catch ( IOException e ) { }