The principle advantage of using JAXB when marshalling and demarshalling XML is the programming model. Simply annotate a few POJOs and use the JAXB API's and you can serialise to XML and deserialise from XML very easily. You don't need to worry about the specifics regarding how the XML is marshalled / unmarshalled. Everything is much simpler than alternatives such as DOM and SAX.
Now data in XML files tends to be hierarchial in nature. For example, conside this XML file:
In this case, the person Barok Obama has a car which is a Green Ford Focus. Here, we see the hierarchial characteristics of XML. The Car is under the Person. In a more sophisticated example, a Person could have a Car, which has a Car Radio, which has an Amplifier, which has Transistors etc. But let's stick with our simpler case for the moment. Suppose we want to unmarshall this XML file using JAXB. We want all the person details (firstname, lastname etc.) and the model of the car belonging to the person. We create a Person POJO and a Car POJO and annotate as appropriate.
To unmarshall this we simply do:
This all seems very simple - especially when you consider that the Car entity doesn't even need any annotations! However, the Car only has one attribute and it can seem like overkill to have a POJO class for something we only want one attribute from! Remember this is a simple example, imagine if the hierarchial structure was much deeper. Something like an outer entity containing an entity, which contained another entity which contained even another entity and all we wanted was the outer entity and one attribute from very deepest nested entity. It's essentially the same problem but just even more overkill. We would have to ensure there were POJO class for everything in the hierarchy - even for entities which we wanted nothing from. No-one likes code bloat. So what can we do?
Well the first thing we gotta remember is that JAXB is a specification for which there are many implementations for (e.g. JaxMeAPI, MOXy, Metro). If we were to use the JAXB reference implementation (shipped with the JDK, there is not much we can do). We have to have a Car and Person POJO. However, if we use the MOXy implementation from EclipseLink we can use some of its extensions to help us. More specifically we can use the MOXy @XmlPath extension which is inspired from XPath.
Let's see it in action. Here is the updated Person POJO.
So where's the Car POJO gone? Well it's deleted. We don't need it anymore. Bye bye.
Using the MOXy @XmlPath annotation we do not need the Car POJO. This annotation resides in org.eclipse.persistence.oxm.annotations package and to get that on your classpath is really simple. If you are a maven user just add:
To tell your JDK to use MOXy for the JAXB implementation at runtime you put a file named
jaxb.properties in the same directory as your JAXB POJOs. It contains one line:
To ensure you are using the MOXy implementation just check the JAXB context:
You should see something like:
After that there are no changes. The exact same unmarshalling code can be used.
One reason why I really like this extension is because it means less code. This usually means cleaner code and more maintable code. This becomes even more obvious in more complex scenarios where entities are much more deeper in hiearchial structure than this simple example. It doesn't matter if you are using something like XJC to generate your POJOs you still got code bloat.
Remember JAXB set out to be a cleaner programming model than JAXP alternatives such as SAX and DOM but in scenarios with deep hierachies, the profileration of classes using JAXB doesn't make it a convincingly cleaner. Remember, it would be quite easy to ignore the classes you don't want using DOM and XPath or even just using SAX.
MOXy swings the battle for cleanliness back to JAXB by providing the ability to use XPath expressions for anything in our XML file.
Note: MOXy has just being included as JAXB implementation for WebLogic 12c.
References:
1. MOXy project page
2. Blaise Doughan's blog
Some simple bits and pieces about Software Architecture. Generally, a JVM is somewhere...
Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts
Monday, January 2, 2012
Monday, April 25, 2011
Those any and anyAttibute elements?
When are the any and anyAttribute tags used in a schema?
Well let's start with the any. Ok, let's say you want to write a schema for sending messages but you want to make it flexible. You want to define the message envelope but the structure of the message payload is unknown to the system. Here's how you'd do it.
This means the message must have a Date, a Sender and any content.
Here's an example message:
The any tag can be further customised with attributes such as minOccurs and maxOccurs.
Now for that fella?
anyAttribute is very similar except it works on attributes.
For example:
Allows the following XML
References:
1. The XSD schema standard
Well let's start with the any. Ok, let's say you want to write a schema for sending messages but you want to make it flexible. You want to define the message envelope but the structure of the message payload is unknown to the system. Here's how you'd do it.
This means the message must have a Date, a Sender and any content.
Here's an example message:
The any tag can be further customised with attributes such as minOccurs and maxOccurs.
Now for that
anyAttribute is very similar except it works on attributes.
For example:
Allows the following XML
References:
1. The XSD schema standard
What are the differences between JAXB 1.0 and JAXB 2.0
What are the differences between JAXB 1.0 and JAXB 2.0?
References: http://javaboutique.internet.com/tutorials/jaxb/index3.html
- JAXB 1.0 only requires JDK 1.3 or later. JAXB 2.0 requires JDK 1.5 or later.
- JAXB 2.0 makes use of generics and thus provides compile time type safety checking thus reducing runtime errors.
- Validation is only available during marshalling in JAXB 1.0. Validation is also available during unmarshalling in JAXB 2.0.
- Termination occurs in JAXB 1.0 when a validation error occurs. In JAXB 2.0 custom ValidationEventHandlers can be used to deal with validation errors.
- JAXB 2.0 uses annotations and supports bi-directional mapping.
- JAXB 2.0 generates less code.
- JAXB 1.0 does not support key XML Schema components like
anyAttribute, key, keyref, andunique. It also does not support attributes likecomplexType.abstract, element.abstract, element.substitutionGroup, xsi:type, complexType.block, complexType.final, element.block, element.final, schema.blockDefault, andschema.finalDefault. In version 2.0, support has been added for all of these schema constructs.
References: http://javaboutique.internet.com/tutorials/jaxb/index3.html
XPath Example
XPath is a query language for XML defined by the W3C. It can:
An example please?
No problem. Let's start with some XML.
The XML represents some books in a library. Suppose you wish to query this XML to see the names of the books that Brian Smith has wrote. Now you don't want the hassle of converting the XML to book objects and filtering through them yourself just to get the names. So you decide to use XPath. Here's how you'd do it.
Other points worth pointing out:
- Select nodes in a XML document
- Compute values from the content of an XML document.
An example please?
No problem. Let's start with some XML.
The XML represents some books in a library. Suppose you wish to query this XML to see the names of the books that Brian Smith has wrote. Now you don't want the hassle of converting the XML to book objects and filtering through them yourself just to get the names. So you decide to use XPath. Here's how you'd do it.
Other points worth pointing out:
- An XPath expression is not thread-safe and not reentrant. In other words, it is the application's responsibility to make sure that one
XPathExpressionobject is not used from more than one thread at any given time, and while theevaluatemethod is invoked, applications may not recursively call theevaluatemethod. - XPathConstants.NODESET is a XPath 1.0 type.
- There are some tools out there to help you figure out what your XPath expression should be. For example XmlToolBox
Subscribe to:
Posts (Atom)