Feeds

A practical guide to JAXB 2.0

Take a peek at its new features

  • alert
  • submit to reddit

Choosing a cloud hosting partner with confidence

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); 

Business security measures using SSL

More from The Register

next story
'Windows 9' LEAK: Microsoft's playing catchup with Linux
Multiple desktops and live tiles in restored Start button star in new vids
Not appy with your Chromebook? Well now it can run Android apps
Google offers beta of tricky OS-inside-OS tech
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Greater dev access to iOS 8 will put us AT RISK from HACKERS
Knocking holes in Apple's walled garden could backfire, says securo-chap
NHS grows a NoSQL backbone and rips out its Oracle Spine
Open source? In the government? Ha ha! What, wait ...?
Google extends app refund window to two hours
You now have 120 minutes to finish that game instead of 15
Intel: Hey, enterprises, drop everything and DO HADOOP
Big Data analytics projected to run on more servers than any other app
prev story

Whitepapers

Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.
Saudi Petroleum chooses Tegile storage solution
A storage solution that addresses company growth and performance for business-critical applications of caseware archive and search along with other key operational systems.
Security and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.