Feeds

A practical guide to JAXB 2.0

Take a peek at its new features

  • alert
  • submit to reddit

Combat fraud and increase customer satisfaction

Create an ArticleType object, which represents an article element. Set the edition, title and author values. Obtain a List object of parameter type ArticleType and add the ArticleType object to the List. Similarly, add another ArticleType object to the Java object representation. Next, obtain a JAXBElement object of parameter type CatalogType using a factory object. A JAXBElement object is a JAXB representation of an XML element.

JAXBElement<CatalogType> catalogElement=factory.createCatalog(catalog); 

Marshal the Java object using the marshal() method.

marshaller.marshal(catalogElement, new FileOutputStream(xmlDocument)); 

To run the JAXBMarshaller.java application in Eclipse, right-click on the application node and select Run As>Run. File catalog.xml gets generated. Refresh the JAXB2 project with File>Refresh. The XML file catalog.xml gets added to the JAXB2 project.

Mapping Java to XML Using Annotations

JAXB 2.0 has added the provision to marshal a Java object to an XML document using annotations. The annotations are defined in the javax.xml.bind.annotation package. In this section we shall generate an example XML document, catalog2.xml (resources zip file), from a Java object using JAXB 2.0 annotations. The corresponding schema representation is listed in catalog2.xsd (resources zip file).

To create the XML document, create an annotated class, Catalog.java (resources zip file). Create root element of the XML document with @XmlRootElement annotation. Create a complexType using @XmlType annotation.

@XmlRootElement
@XmlType(name="", propOrder={"publisher", "edition", "title", "author"})

The annotation element name is specified as an empty string, because the complexType is defined within an element. The element order is specified using propOrder annotation element. In the Catalog class define constructors for the class, the different JavaBean properties (edition, title, author). Root element catalog has an attribute journal. Define the journal attribute using @XmlAttribute annotation.

@XmlAttribute
public String journal; 

Define getter and setter methods for the different properties and the journal attribute. Add a Java class, Catalog.java, to the JAXB2 project in Eclipse. The directory structure of the JAXB2 project with the Catalog.java class is shown in Figure 8.

JAXB2 Project Directory Structure

In the marshalling class JavaToXML.java create a JAXBContext using newInstance() method with Catalog.class as argument to the method. Create a Catalog class object and set the values of elements and attributes. Marshal the Catalog object with a Marshaller object. Run the JavaToXML.java class in Eclipse. The XML document gets created. Java classes may also be mapped to XML Schemas.®

Click here for the resources file (zip)

Combat fraud and increase customer satisfaction

More from The Register

next story
OpenBSD founder wants to bin buggy OpenSSL library, launches fork
One Heartbleed vuln was too many for Theo de Raadt
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Ubuntu 14.04 LTS: Great changes, but sssh don't mention the...
Why HELLO Amazon! You weren't here last time
Got Windows 8.1 Update yet? Get ready for YET ANOTHER ONE – rumor
Leaker claims big release due this fall as Microsoft herds us into the CLOUD
Patch iOS, OS X now: PDFs, JPEGs, URLs, web pages can pwn your kit
Plus: iThings and desktops at risk of NEW SSL attack flaw
Next Windows obsolescence panic is 450 days from … NOW!
The clock is ticking louder for Windows Server 2003 R2 users
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Apple inaugurates free OS X beta program for world+dog
Prerelease software now open to anyone, not just developers – as long as you keep quiet
prev story

Whitepapers

Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.