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:
Now among all the suggested approaches what is the BEST approach to go convert Java Objects to XML
and XML to Java Objects
?
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.
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.
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 .
JAXB is the standard and the best approach for coverting Java objects to XML. There are several open source implementations available:
For more information on JAXB check out my blog:
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:
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:
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:
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:
Use Case #5 - XML Infoset Preservation:
In this use case you need to preserve the unmapped content: comments, processing instructions etc.
Nominees:
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:
Use Case #7 - Compatibility with XML Web Services (JAX-WS)
JAXB is the default binding layer for JAX-WS.
Nominees:
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:
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:
Nominees:
Other Things to Consider
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With