Feeds

A practical guide to JAXB 2.0

Take a peek at its new features

  • alert
  • submit to reddit

The Power of One Brief: Top reasons to choose HP BladeSystem

Unmarshalling an XML Document

In this section we shall unmarshal the example XML document, catalog.xml. In the JAXBUnMarshaller.java class create a JAXBContext object, which provides a JAXB context for implementing JAXB binding framework operations. A JAXBContext object is initialised with a colon-separated list of Java package names, which consist of schema-derived classes and user annotated classes.

JAXBContext jaxbContext = JAXBContext.newInstance("generated");

Create an Unmarshaller object, which is used to convert an XML document to a Java object.

Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

If the application were a JAXB 1.0 application, we would have set the Unmarshaller to be validating using setValidating()method. The setValidating() method has been deprecated in JAXB 2.0. Schema validation in JAXB 2.0 is performed using JAXP 1.3 validation API. Create a SchemaFactory object and create a Schema object from the SchemaFactory object. Set the schema on the Unmarshaller object. Error handling in JAXB 2.0 is implemented using the ValidationEventHandler interface. To add error handling to the example application, create a class CustomValidationEventHandler that implements the ValidatonEventHandler interface. Create a ValidationEventHandler object and set the ValidationEventHandler object on the Unmarshaller object.

Unmarshal using the unmarshal() method, which returns a parameterized JAXBElement object. The parameter type is CatalogType in the example application.

JAXBElement<CatalogType> catalogElement =(JAXBElement<CatalogType>) unMarshaller.unmarshal(xmlDocument); 

Obtain a CatalogType object from the JAXBElement object.

CatalogType catalog=catalogElement.getValue();

Retrieve the journalTitle and publisher values. Obtain a parameterized list of parameter type JournalType. Iterate over the List object and obtain a parameterized list of parameter type ArticleType for each of the JournalType objects. Iterate over the parameterized list of parameter type ArticleType and output values for edition, title and author.

To run the JAXBUnMarshaller.java application in the Eclipse project JAXB2, right-click on the JAXBUnMarshaller.java application and select Run As>Run. Output generated by running the application in Eclipse is shown in Figure 7.

Output from Unmarshalling

Marshalling an XML Document

In this section we shall marshal an XML document, catalog.xml. In the JAXBMarshaller.java class create a JAXBContext object with the procedure similar to the previous section. Create a Marshaller object, which is used to marshal a Java object to an XML document.

Marshaller marshaller = jaxbContext.createMarshaller();

To produce formatted output, set the Marshaller property jaxb.formatted.output to true.

marshaller.setProperty("jaxb.formatted.output",new Boolean(true)); 

Next, create a Java object representation of the XML document to be marshalled. Create an ObjectFactory object, which is used to initialise the Java object

ObjectFactory factory = new ObjectFactory();

Create a CatalogType object, which represents root element catalog.

CatalogType catalog = factory.createCatalogType();

Set the journalTitle and publisher attributes of root element catalog. Create a JournalType object, which represents a journal element. Get a list of parameter type JournalType from the CatalogType object and add the JournalType object to the List object.

JournalType journal = factory.createJournalType();
List<JournalType> journalList = catalog.getJournal();
journalList.add(journal); 

Seven Steps to Software Security

More from The Register

next story
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Application security programs and practises
Follow a few strategies and your organization can gain the full benefits of open source and the cloud without compromising the security of your applications.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Securing Web Applications Made Simple and Scalable
Learn how automated security testing can provide a simple and scalable way to protect your web applications.