Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Generate an XML Document from an Object Model with JAXB 2 : Page 4

Learn how to get an XML document from your Java object model using JAXB 2. With annotations, you can also customize the document and use adapters to format data.


advertisement
Unmarshal and Generate a Schema
As Figure 1 showed, JAXB can also be used to unmarshal, generate, and compile a schema. So let's quickly talk about that. From what you've learned so far, you should understand unmarshalling. The idea is to take the previously obtained XML document and generate an object graph. For that, you get a JAXBContext, from which you create an Unmarshaller object. You then call the unmarshal method, passing an XML document (either from a String or a file), and it returns an instance of an Individual with its attributes:

// xmlString contains the XML document of an individual StringReader reader = new StringReader(xmlString); JAXBContext context = JAXBContext.newInstance(Individual.class); Unmarshaller u = context.createUnmarshaller(); Individual individual = (Individual) u.unmarshal(reader); System.out.println(individual.getFirstname());

An XML schema describes the structure of an XML document and it is written in XML syntax. Even if you don't know much about XML schema, you can generate one using the schemaGen tool provided by Sun's JAXB implementation (command line or Ant task). For example, schemaGen can take all the classes developed for this article and generate the XML schema in Listing 3. In that schema, you can see the Address, Company, Individual, and Tag classes described as complex types (download the code and you will see how schemaGen is invoked from Ant):



<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="address"> <xs:sequence> <xs:element name="street" type="xs:string" minOccurs="0"/> <xs:element name="zip" type="xs:string" minOccurs="0"/> <xs:element name="city" type="xs:string" minOccurs="0"/> <xs:element name="country" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="company"> <xs:sequence> <xs:element name="contactName" type="xs:string" minOccurs="0"/> <xs:element name="telephone" type="xs:string" minOccurs="0"/> <xs:element name="email" type="xs:string" minOccurs="0"/> <xs:element name="numberOfEmployees" type="xs:int" minOccurs="0"/> <xs:element name="homeAddress" type="address" minOccurs="0"/> <xs:element name="delivery" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="address" type="address" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:long"/> <xs:attribute name="name" type="xs:string"/> </xs:complexType> <xs:complexType name="individual"> <xs:sequence> <xs:element name="firstname" type="xs:string" minOccurs="0"/> <xs:element name="dateOfBirth" type="xs:string" minOccurs="0"/> <xs:element name="telephone" type="xs:string" minOccurs="0"/> <xs:element name="email" type="xs:string" minOccurs="0"/> <xs:element name="homeAddress" type="address" minOccurs="0"/> <xs:element name="delivery" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="address" type="address" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:long"/> <xs:attribute name="lastname" type="xs:string"/> </xs:complexType> <xs:complexType name="tag"> <xs:sequence> <xs:element name="addresses" type="address" nillable="true" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="name" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="dateAdapter"> <xs:complexContent> <xs:extension base="xmlAdapter"> <xs:sequence/> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="xmlAdapter" abstract="true"> <xs:sequence/> </xs:complexType> </xs:schema>

When you download JAXB, another tool comes along with it: the schema compiler (xjc). schemaGen allows to generate an XML schema from Java classes, and xjc does the opposite: from an XML schema, it creates annotated Java files. You can use it as a command line or as an Ant task. Take Listing 3, run xjc, and that will generate the class hierarchy (not the objects, the classes) that describes the Watermelon business model.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap