December 21, 2010

Represent String Values as Element Names with JAXB ("foo" as <foo/>)

In this example we will map a String value to an element name.  Instead of <foo>bar</foo> we want <bar/>.  We will accomplish this by using @XmlAdapter and @XmlElementRef.

December 20, 2010

JAXB and Marshal/Unmarshal Schema Validation

In a previous post I described how to validate an object model (mapped with JAXB annotations) against an XML schema using the javax.xml.validation APIs.  In this post I'll describe how to leverage those APIs during unmarshal and marshal operations.

December 10, 2010

Extending JAXB - Representing Metadata as XML

In JAXB (JSR-222) metadata is applied to Java classes via standard annotations. There are times when using annotatations is not practical, and an alternate mechanism is required.  In this post I'll demonstrate how EclipseLink JAXB (MOXy) can leverage XML to represent the JAXB metadata.

An XML metadata representation is useful when:
  • You cannot modify the domain model (it may come from a 3rd party).
  • You do not want to introduce compile dependencies on JAXB APIs (if you are using a version of Java prior to Java SE 6).
  • You want to apply multiple JAXB mappings to a domain model (you are limited to one representation with annotations).
  • Your object model already contains so many annotations from other technologies that adding more would make the class unreadable.

December 7, 2010

Case Insensitive Unmarshalling with JAXB

Recently I've been asked in a couple different forums how to handle case insensitive unmarshalling in JAXB.  In this post I'll discuss an approach using a StAX StreamReaderDelegate.

I am considering adding this as an official feature, and would be interested in your feed back.  I am using the following bug to track this issue:

December 3, 2010

JAXB and Immutable Objects

I was recently sent a blog post:  JAXB and immutable objects, by Aniket Shaligram.  In this post the author addresses the issue by adding a default constructor and using field access, to avoid adding set methods.  This approach is perfectly valid, but leaves the class being more mutable than some people like (including the person who sent me the link).  In this post I'll discuss an alternative approach using XmlAdapter.

November 24, 2010

Validate a JAXB Object Model With an XML Schema

In this post I will demonstrate how to validate a JAXB object model against an XML schema.  This will be done using the javax.xml.validation APIs.

November 5, 2010

JAXB and Inheritance - MOXy Extension @XmlDescriminatorNode/@XmlDescrimintatorValue

In previous blog posts I described how to leverage the xsi:type attribute and substitution groups to represent inheritance.  In this post I'll demonstrate how an upcoming EclipseLink 2.2 JAXB (MOXy) extension can be used to represent inheritance.  It leverages an attribute of your own choosing to represent the subtype.  If you are using a version of EclipseLink prior to 2.2 you can use the technique described here.

November 4, 2010

JAXB and Inheritance - Using Substitution Groups

In a previous blog post I described how to leverage the xsi:type attribute to represent inheritance.  In this post I'll demonstrate how to use the element name instead by leveraging the XML Schema concept of substitution groups.

JAXB and Inheritance - Using the xsi:type Attribute

In a previous blog post I was challenged by an Xstream user to describe how JAXB handles inheritance.  In this post I'll describe one strategy that can be used, and how easily this can be done with JAXB.  I'll discuss alternate strategies in future posts.

October 25, 2010

How Does JAXB Compare to Simple?

In a previous post I compared JAXB to XStream.  In this post I'll run that comparison with the Simple XML Serialization framework.

October 21, 2010

JAXB and XSD Choice: @XmlElements

XML Schema has the concept of a choice element.  The choice element indicates that one of the elements defined within the choice may occur at this point in the XML document.  In this example I will demonstrate how easily this can be mapped using JAXB.

October 7, 2010

JAXB and Shared References: @XmlID and @XmlIDREF

When converting objects to XML, privately owned data can easily be represented through nested elements.  When there are multiple references to an object another mechanism needs to be used.  In JAXB this mechanism is @XmlID and @XmlIDREF.

October 6, 2010

How Does JAXB Compare to XStream?

The XStream FAQ states the following when being compared to JAXB:

JAXB is a Java binding tool. It generates Java code from a schema and you are able to transform from those classes into XML matching the processed schema and back. Note, that you cannot use your own objects, you have to use what is generated.
In this post I will provide an alternative answer to that question.

September 28, 2010

JAXB & Collection Properties

In this post will examine the different options JAXB offers for representing collections in XML.  We will look at the following annotations:
  • @XmlElement
  • @XmlElementWrapper
  • @XmlList
  • @XmlList and @XmlAttribute
  • @XmlList and @XmlValue

JAXB & XML Infoset Preservation

In this post we will discuss how to use JAXB to preserve the XML Infoset. 

September 13, 2010

XPath Based Mapping - Geocode Example

In a previous post I introduced MOXy's XPath based mapping JAXB extension.  In this post I'll use Google Maps Geocoding API V2 to demonstrate how powerful this extension is.

September 10, 2010

Processing Atom Feeds with SDO

Service Data Objects (SDO) is a Java standard (JSR-235) that can be used to process XML.  In this post we will compare SDO to JAXB by processing an Atom feed.

September 8, 2010

Processing Atom Feeds with JAXB

Atom is an XML format for representing web feeds.  A standard format allows reader applications to display feeds from different sources.  In this example we will process the Atom feed for this blog.

August 26, 2010

EclipseLink 2.1.1 Patch Set Released

The first patch set for the EclipseLink 2.1 stream is now available.  EclipseLink 2.1.1 can be downloaded here:

The following XML binding layer bugs were fixed:

Using @XmlAnyElement to Build a Generic Message

In this post we are building our own XML messaging framework.  We want to have a standard message object (similar to a SOAP envelope) that the framework can understand, and the ability to easily add domain specific payloads over time.

August 25, 2010

@XmlTransformation - Going Beyond XmlAdapter

XML Adapter is my favourite JAXB feature, which is why it was the topic of my first post XmlAdapter - JAXB's Secret Weapon.  With it there is no such thing as an unmappable object in JAXB.  However today I came across a use case where the MOXy's transformation mapping was a much better fit.

August 23, 2010

Creating a RESTful Web Service - Part 5/5

In this post we will examine how a client may interact with the RESTful service we created in part 4

August 20, 2010

Creating a RESTful Web Service - Part 4/5

Java API for RESTful Web Services (JAX-RS) is the Java EE standard for creating RESTful Web Services.  In this post we will create a RESTful service from an EJB session bean using JAX-RS.

Creating a RESTful Web Service - Part 3/5

Java Architecture for XML Binding (JAXB) is the Java EE standard for mapping POJOs to XML.  In this example we will use JAXB to apply an XML representation to the JPA entities we created in part 2.  Since we are mapping JPA entities to XML we will use the EclipseLink JAXB  (MOXy) implementation and leverage some of its extensions.

Some developers will maintain separate JPA and JAXB models, and perform a copy step to move data between them.  This can be quite a painful and error prone process.  Luckily MOXy has a number of extensions that make this unnecessary:

August 19, 2010

Creating a RESTful Web Service - Part 2/5 (Annotations)

Java Persistence Architecture (JPA) is the Java EE standard for mapping POJOs to a relational database. In this example we will use JPA to interact with our database data we set up in part 1.

August 18, 2010

Creating a RESTful Web Service - Part 1/5

I have been doing a lot of work with data access services recently so I figure it's time to share what I have discovered.  Don't be scared off that this series is broken into 5 parts, I will keep them short:

In this series of posts we will use a number of standard Java EE technologies to quickly create a RESTful data access service:

August 17, 2010

XML Binding Talk at JavaOne

This year I will be doing a talk on XML Binding at JavaOne in San Francisco.  I will be pitting the two XML binding standards:  JAXB  (JSR-222) & SDO (JSR-235) against each other to help you determine which one is right for you.  I hope to see you there.


Effective XML: Leveraging JAXB and SDO (S314491)
Wednesday September 22, 1-2pm
Hilton San Francisco, Yosemite A

August 11, 2010

JAXB & Namespaces

The majority of XML documents are namespace qualified, yet most XML binding examples do not demonstrate namespaces (sometimes because the XML binding solution does not support them). In this post I'll demonstrate how easy namespace handling is using JAXB.

July 26, 2010

JPA Entities to XML - Bidirectional Relationships

JAXB is your best choice when binding Java objects to XML.  But what happens when those Java objects turn out to be JPA entities?  For the most part it is business as usual, although there are a couple things to be aware of.  In this post we'll cover a MOXy extension that can be used to map a bidirectional relationship.

July 23, 2010

MOXy JAXB - Map Interfaces to XML

I recently came across a Twitter message (tweet) complaining that JAXB does not support binding Java interfaces to XML. Below I'll describe how this can be accomplished using MOXy in the upcoming EclipseLink 2.1.1 and 2.2 releases (try it now).

July 20, 2010

JAXB - The XML Binding Standard

Many people think of JAXB as an implementation (the reference implementation included in Java SE 6).  In reality JAXB is a Java Community Process (JCP) specification with multiple implementations (such as EclipseLink MOXy).  In this post I'll discuss the importance of JAXB the standard.

July 14, 2010

XPath Based Mapping

In this post we'll explore the XPath based mapping aspect of EclipseLink JAXB (MOXy).  XPath based mapping is what allows an existing object model to be mapped to an existing XML schema.  MOXy is the only XML binding solution I'm aware of with this ability and therefore the only true object-to-XML mapper (OXM).

July 13, 2010

CDATA, CDATA Run, Run Data Run

Recently the topic of JAXB's handling of CDATA has come up on a few separate Twitter messages (tweets). In this post I will describe how to handle CDATA using EclipseLink JAXB (MOXy).

July 12, 2010

XmlAdapter - JAXB's Secret Weapon

The XmlAdapter mechanism in JAXB ensures that there is no such thing as an unmappable class (with a little programmatic help). However there appears to be some confusion on how to use XmlAdapter, below is the general concept:
  1. Identify the unmappable class
  2. Create an equivalent class that is mappable
  3. Create an XmlAdapter to convert between unmappable and mappable objects
  4. Specify the XmlAdapter
Part of the problem may be due to the Javadocs on the XmlAdapter class. Below I'll flush out the example used there.