Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java to XML conversions? [closed]

What are different approaches to convert Java Objects to XML, I know one option is JAXB but would like to know what are the other approaches/tools available for the same ?

Note: I do not have further requirements and so I cannot add more meat to the question but at this point of time it would be really great if I can get an idea of what different approaches are available for converting Java to XML ?

Update: Different suggested approaches are:

  1. javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller
  2. XStream
  3. XMLBean
  4. JAXB
  5. Castor
  6. JIBX
  7. Apache Digester

Now among all the suggested approaches what is the BEST approach to go convert Java Objects to XML and XML to Java Objects ?

like image 469
Rachel Avatar asked Nov 20 '10 00:11

Rachel


People also ask

Can we convert String to XML in Java?

Document convertStringToDocument(String xmlStr) : This method will take input as String and then convert it to DOM Document and return it. We will use InputSource and StringReader for this conversion.

Can we convert JSON to XML in Java?

We can convert a JSONObject into an XML format using org. json. XML class, this provides static methods to convert an XML text into a JSONObject and to convert a JSONObject into an XML text.

What is @XmlRootElement in Java?

When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .


2 Answers

JAXB is the standard and the best approach for coverting Java objects to XML. There are several open source implementations available:

  • EclipseLink MOXy (I'm the tech lead)
  • Metro (The reference implementation, included in Java SE 6)
  • JaxMe

For more information on JAXB check out my blog:

  • http://bdoughan.blogspot.com

UPDATE:

What is the BEST approach?

This ultimately depends on what you are trying to do, I'll explain below:

Use Case #1 - Starting from an XML Schema

In this use case you have an XML schema and you want to generate a Java model. Not many of the tools mentioned in this thread support this use case. XStream for example recommends XMLBeans for this.

Nominees:

  • JAXB (all implementations) - Generates POJOs with JAXB annotations.
  • XMLBeans - Generates proprietary classes that include XML binding logic.

Use Case #2 - Starting from Java Classes (that you can edit)

In this use case you have much more selection (only XMLBeans is eliminated). The edits normally involve the addition of annotations to control the mapping.

Nominees:

  • Everyone but XMLBeans

Use Case #3 - Starting form Java Classes (that you can not edit)

In this use case you do not have the source to modify the model classes. This requires the metadata to be supplied externally either with an XML file of by code.

Nominees:

  • EclipseLink JAXB (MOXy) - Offers an external binding file, and metadata can be applied programmatically.
  • Metro JAXB - Can leverage Annox or JAXBIntroductions
  • Castor - Offers an external binding file
  • JiBX - Offers an external binding file
  • XStream - Metadata can be applied programmatically

Use Case #4 - Meet-in-the-Middle (Existing classes and schema)

In this use case you have existing classes that you need to map to an existing XML schema. EclipseLink MOXy with its XPath based mapping is the only tool I'm aware of that can handle this use case

Nominees:

  • EclipseLink JAXB (MOXy)

Use Case #5 - XML Infoset Preservation:

In this use case you need to preserve the unmapped content: comments, processing instructions etc.

Nominees:

  • JAXB (all implementations) - Has the Binder feature.
  • XMLBeans - The generated object model stores the entire XML infoset.

Use Case #6 - Compatibility with JPA

JPA is the Java standard for Java persistence. JPA has many concepts: composite keys, bidirectional relationships, lazy loading, etc that can be hard to use with an XML binding solution. For example any XML tool that only interacts with objects via the field will usually have problems with lazy loading properties.

Nominees:

  • EclipseLink JAXB (MOXy) - Was built with JPA in mind.

Use Case #7 - Compatibility with XML Web Services (JAX-WS)

JAXB is the default binding layer for JAX-WS.

Nominees:

  • JAXB (implementation depends of the JAX-WS provider)

Use Case #8 - Compatibility with RESTful Web Services (JAX-RS)

JAX-RS offers a light-weight alternative to JAX-WS based on the HTTP protocol. Check out the following for an example.

Nominees:

  • JAXB (all implementations) - The default binding layer and easiest to use with JAX-RS.
  • Everything else - You can leverage the concepts of MessageBodyReader/Writer to use other XML tools.

Use Case #9 - Compatibility with Spring

Spring has some built in support for integrating with XML binding tools, check out the following link for more information:

  • http://static.springsource.org/spring-ws/docs/0.9.1/reference/oxm.html

Nominees:

  • JAXB (all implementations)
  • Castor
  • XMLBeans
  • JiBX

Other Things to Consider

  • Is the tool still being developed/supported? As funny as this sounds I've seen people recommend tools that haven't beed updated in 5 years. Some of the tools mentioned here haven't released in 2 years.

My Pick for BEST approach? - JAXB

Looking at the above categories, JAXB may not always be the best fit for a particular use case (but it is always a good fit), but it is the only library that can be used for all the use cases. This means it can always do the job. The alternative is to use different libraries for different tasks giving you multiple libraries to support.

I do lead a JAXB implementation EclipseLink MOXy, but MOXy began its life as a proprietary XML binding library TopLink OXM. TopLink has always understood the benefit of standards (i.e. EJB/JPA), and we implemented JAXB 1. Then we (I am the represetative) became active members on JAXB 2 (JSR-222).

like image 89
bdoughan Avatar answered Sep 24 '22 02:09

bdoughan


You can always do it yourself with javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller interfaces.

This isn't as crazy as it sounds. It's not that hard to do, and you'll have complete control over and knowledge of what's done.

I'm willing to acknowledge that lots of folks prefer an automated approach. My offering won't be popular.

I've used XStream in the past and liked it, but when I've used it I didn't have to worry about XML namespaces. I'm told that XStream doesn't deal with them well.

like image 29
duffymo Avatar answered Sep 23 '22 02:09

duffymo