Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best Java OXM library?

Tags:

java

oxm

Even though I've been a developer for awhile I've been lucky enough to have avoided doing much work with XML. So now I've got a project where I've got to interact with some web services, and would like to use some kind of Object-to-XML Mapping solution.

The only one I'm aware of is JAXB. Is that the best to go with? Are there any other recommendations?

One catch - I'm stuck using Java 1.4, so I can't do anything with annotations.

like image 599
bpapa Avatar asked Jan 05 '09 16:01

bpapa


People also ask

What is Oxm in Java?

Spring OXM stands for Spring Object XML Mappers and it is a module available in Spring to ease the mapping between java objects and XML documents. The module is extensible and hence it provides integration with various popular frameworks like Castor, JAXB, XmlBeans and XStream.

What is marshal XML?

Marshalling provides a client application the ability to convert a JAXB-derived Java object tree back into XML data. By default, the Marshaller uses UTF-8 encoding when generating XML data. Client applications are not required to validate the Java content tree before marshalling.


3 Answers

JAXB is the best choice:

  • Public API included in Java SE 6
  • Binding layer for JAX-WS (Web Services)
  • Binding layer for JAX-RS (Rest)
  • Can preserve XML Infoset
  • Multiple implementations: Metro, MOXy, JaxMe, etc

EclipseLink JAXB (MOXy) is the best implementation:

MOXy is a JAXB implementation with Extensions

MOXy has an external configuration file (based on JAXB annotations with extensions):

  • http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML

Has XPath based mapping, for deep mapping:

  • http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
  • http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html
  • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html

Designed to handle ORM mapped objects, including support for bidirectional relationships:

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
  • http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
like image 194
bdoughan Avatar answered Oct 10 '22 12:10

bdoughan


If you're calling a web-service with a WSDL, JAXB is absolutely the best option. Take a look at wsimport, and you're be up and running in 10 minutes.

I don't think JAXB 2.0 will be possible on Java 1.4. You may need to use Axis instead:

java -cp axis-1.4.jar;commons-logging-1.1.jar;commons-discovery-0.2.jar;jaxrpc-1.1.jar;saaj-1.1.jar;wsdl4j-1.4.jar;activation-1.1.jar;mail-1.4.jar org.apache.axis.wsdl.WSDL2Java http://someurl?WSDL

This will generate similar stubs to JAXB.

If you don't have a WSDL or XSD, you can always generate one.

like image 27
Chase Seibert Avatar answered Oct 10 '22 10:10

Chase Seibert


There's XStream. I seem to remember I used that ages ago, and it was fine. Can't say I have enough experience to recommend it for or against, but it's worth checking out just as an alternative.

like image 3
Jon Skeet Avatar answered Oct 10 '22 12:10

Jon Skeet