User: Password:
   Keep me logged in.
Register  |  I forgot my password

Login  |  Register

Efficient Java Tools  - Listing Details

ID:210
Title:JiBX: Binding XML to Java Code
Pagerank:5
Short Description:
Listing Thumbnail

JiBX is a tool for binding XML data to Java objects. It's extremely flexible, allowing you to start from existing Java code and generate an XML schema, start from an XML schema and generate Java code, or bridge your existing code to a schema that represents the same data. It also provides very high performance, outperforming all other Java data binding tools across a wide variety of tests.

Description:

How does JiBX manage to provide both flexibility and performance? The key is using binding definition documents to specify how your Java objects are converted to or from XML, combined with bytecode enhancement to embed the conversion code directly into your classes. The bytecode enhancement is done by executing one of the JiBX components (the binding compiler) after your Java classes have been compiled. Once the binding compiler has run and your classes have been enhanced with the JiBX binding code, you can continue the normal steps you take in assembling your application (such as building jar files, etc.).

The second JiBX component is the binding runtime. The enhanced class files generated by the binding compiler use this runtime component both for actually building objects from an XML input document (called unmarshalling, in data binding terms) and for generating an XML output document from objects (called marshalling). The runtime uses a separate XML parser (either one based on the XMLPull open source API, or on the StAX Java standard), but is otherwise self-contained.

Performance was originally part of the inspiration for writing JiBX, and JiBX has consistently delivered performance far ahead of the field. We're not aware of any recent published performance comparisions between data binding frameworks, but you can view some older results from the BindMark tests, along with a similar study focused around Web services performance.. These sets of results are both from late 2005, but our own testing shows that little has changed since then.

Map handling

Another useful class in jibx-extras.jar supports marshalling and unmarshalling of java.util.Map instances. This is the org.jibx.extras.HashMapperStringToComplex class. The support it provides is limited to maps with java.lang.String keys and object values with mappings defined by the binding (using elements), and the only flexibility it provides is in choosing the name of the root element, but it's still useful for many cases. It's also intended as a sample implementation that can be modified for your own particular requirements. For more discussion of both this specific code and the use of custom marshallers and unmarshallers in general, see the binding tutorial section on  The implementation included in jibx-extras.jar includes some nice customization features. By subclassing this implementation in your own code you can override the default names used for the item wrapper element and the key attribute, as well as control whether an item count is included and the name used for that attribute if so.

ID and IDREF handlers

The org.jibx.extras.IdRefMapperBase and  rg.jibx.extras.IdDefRefMapper Base  classes support special handling of objects using ID and IDREFs. These differ from the other classes in jibx-extras.jar in that they're abstract base classes which need to be subclassed by the user in order to construct usable marshaller/unmarshallers. The flexibility they provide is often worth the extra effort, though. org.jibx.extras. IdRefMapperBase lets you use IDREF values directly in a collection, which is not supported by the basic JiBX code. It expects the XML structure for each item to be an element with no content and a single attribute giving the IDREF value (like ). When a subclass is used as the marshaller/unmarshaller for a structure within a collection the referenced objects will be converted to and from the XML IDREF representation. Subclasses can also be used as marshaller/unmarshallers outside of a collection, but in this case you could just as easily use an IDREF value directly. This class only works with IDREF values which have been defined before the point of reference.

Versioned bindings

The org.jibx.extras.BindingSelector class supports marshalling and unmarshalling documents with different binding versions. As with the above classes, the support it provides is limited but useful. In the case of BindingSelector, the root element of the document needs to have some attribute that identifies the particular version used for a document. The version attribute value is used to select the binding to be applied when unmarshalling a document, and a supplied version can also be used when marshalling a document. See the binding tutorial section on Controlling JiBX with front-end code for full details.

Using document models

The extras also include marshaller/unmarshaller classes that allow you to use document models for portions of your documents. This can be useful in situations where documents can contain extension information that's not well structured, or where you need to work with XML document components (such as comments or processing instructions) that are not easily handled with data binding. The classes included currently support dom4j and W3C DOM models.

 

Category:XML
Link Owner:
Date Added:May 02, 2010 06:26:16 PM
Number Hits:28
URL:    http://jibx.sourceforge.net/index.html
Ratings
You must be logged in to leave a rating.
Average rating: (0 votes)
Reviews

No Reviews Yet.


You must be logged in to leave a Comment.