Feeds

A practical guide to JAXB 2.0

Take a peek at its new features

  • alert
  • submit to reddit

Intelligent flash storage arrays

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)

Top 5 reasons to deploy VMware with Tegile

More from The Register

next story
Preview redux: Microsoft ships new Windows 10 build with 7,000 changes
Latest bleeding-edge bits borrow Action Center from Windows Phone
Google opens Inbox – email for people too thick to handle email
Print this article out and give it to someone tech-y if you get stuck
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Entity Framework goes 'code first' as Microsoft pulls visual design tool
Visual Studio database diagramming's out the window
Google+ goes TITSUP. But WHO knew? How long? Anyone ... Hello ...
Wobbly Gmail, Contacts, Calendar on the other hand ...
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Redmond top man Satya Nadella: 'Microsoft LOVES Linux'
Open-source 'love' fairly runneth over at cloud event
prev story

Whitepapers

Choosing cloud Backup services
Demystify how you can address your data protection needs in your small- to medium-sized business and select the best online backup service to meet your needs.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.
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.
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.