Login | Register   
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 2

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
Let's Get Generate Some XML
Watermelon sells and delivers music products such as instruments, amplifiers, scores, books, and so on. In its address book, the company stores two types of customers: individuals and companies. Each has a home address and a set of delivery addresses. Each delivery address can be used on weekends and/or evenings and/or weekdays. I will include this information by adding tags to an address. Figure 2 shows the business model for Watermelon's address book.

Click to enlarge

Figure 2. Business Model for Watermelon's Address Book

Watermelon wants to send the details of some of its customers to a partner in XML format, so it needs an XML document of its object model for a given customer. With JAXB, this is really easy to do. The following code creates an instance of an Individual and then sets its attributes (first name, last name…), a home address, and two delivery addresses that are tagged. Once the objects are all set, it uses the javax.xml.bind.Marshaller to generate an XML representation of the Individual object, which is then displayed:



Listing 1: Creates an XML Representation of an Individual
// Instantiates Tag objects Tag tag1 = new Tag("working hours"); Tag tag2 = new Tag("week-ends"); Tag tag3 = new Tag("mind the dog"); // Instantiates an individual object with home address calendar.set(1940, 7, 7, 0, 0, 0); Individual individual = new Individual(1L, "Ringo", "Starr", "+187445", "ringo@star.co.uk", calendar.getTime()); individual.setHomeAddress(new Address(2L, "Abbey Road", "London", "SW14", "UK")); // Instantiates a first delivery address Address deliveryAddress1 = new Address(3L, "Findsbury Avenue", "London", "CE451", "UK"); deliveryAddress1.addTag(tag1); deliveryAddress1.addTag(tag3); individual.addDeliveryAddress(deliveryAddress1); // Instantiates a second delivery address Address deliveryAddress2 = new Address(4L, "Camden Street", "Brighton", "NW487", "UK"); deliveryAddress2.addTag(tag1); deliveryAddress2.addTag(tag2); individual.addDeliveryAddress(deliveryAddress2); // Generates XML representation of an individual StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(Customer.class); Marshaller m = context.createMarshaller(); m.marshal(individual, writer); System.out.println(writer);

Author's note: The Marshaller.marshal() method takes an object and marshals it into several supports. The example uses a StringWriter to have a string representation of the XML document, but you could use a FileOutputStream to store the XML into a file, as follows:

File file = new File("customer.xml"); FileOutputStream outputFile = new FileOutputStream(file); m.marshal(individual, outputFile);

The code creates a new instance of JAXBContext by using the static method newInstance, to which it passes the root class that the application needs to manipulate (Customer). From the created Marshaller object, it then calls the marshal method that generates the XML representation of an individual into a StringWriter.

The only thing left to do is add an @XmlRootElement annotation to the Customer class. The @XmlRootElement annotation notifies JAXB that the annotated class is the root element of the XML document. If this annotation is missing, JAXB will throw an exception. If you add it and run the code, you will get the following XML document:

Listing 2: XML Representation of an Individual
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <customer> <deliveryAddresses> <city>London</city> <country>UK</country> <id>3</id> <street>Findsbury Avenue</street> <tags> <name>working hours</name> </tags> <tags> <name>mind the dog</name> </tags> <zipcode>CE451</zipcode> </deliveryAddresses> <deliveryAddresses> <city>Brighton</city> <country>UK</country> <id>4</id> <street>Camden Street</street> <tags> <name>working hours</name> </tags> <tags> <name>week-ends</name> </tags> <zipcode>NW487</zipcode> </deliveryAddresses> <email>ringo@star.co.uk</email> <homeAddress> <city>London</city> <country>UK</country> <id>2</id> <street>Abbey Road</street> <zipcode>SW14</zipcode> </homeAddress> <id>1</id> <telephone>+187445</telephone> </customer>

With just one annotation (@XmlRootElement), a Marshaller object, and the coding-by-exception approach that guides JAXB (you need to add custom code only when the default is inadequate), you can easily get an XML representation of your object graph. The root element <customer> represents the Customer object, and it includes all the attributes (one home address, two delivery addresses, an ID, a phone number, and so on).



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap