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.
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
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.
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.
JIBX - known for performance
JAXB2 - Ease of use
Castor - Ease of use
others -
XMLBean,
Xstream
Related
We have a big project which is composed by JEE modules, JAVA client applications, Android applications, and other self-made java projects.
Because of the variety of projects, we decided to make java libs projects and java entity projects which are common to JEE, Java client applications and Android applications in the goal of limit code redundancy between projects.
At the beginning, we only had Java Clients and Restfull web services on the JEE server side which were exchanging data using JAXB XML Binding API. So it was easy to use JAXB annotations on our Classes in the entity project (which is set as dependency on Java Client project and JEE projects). Both sides could easily encode and decode XML data with the same annotations.
Now that we have an Android app, I wanted to use the same way to exchange data. The thing is that JAXB is 'depreciated' on Android. So I found in Jackson lib that there is a JaxbAnnotation parameter for getting data which is bind with JAXB but I'm not convinced by the full compatibility of the solution.
I also tried using JSON binding since I saw that JSON-B will be the standard in JavaEE 8 but it seems that it needs JavaEE API classes and I don't think that it's good to add it to Android project.
So my question is: What is the best practice to exchange data between JEE Restfull web services and Android application using the same entity dependency (and same parsing API) and limiting the XML or JSON binding annotation on the entity objects?
I hope that you will well understand the problem.
Thank you.
Let's name your entities project entities-module. This project contains POJO classes annotated with JAXB annotations such as: #XmlElement, #XmlType, etc. Since, like you wrote, "JAXB is 'depreciated' on Android" you need to choose between: read JAXB annotations by other tools or create new customised POJO structure.
Read JAXB annotations by other tools
Jackson library has good support for JAXB annotations. All you need to do is to use jackson-module-jaxb-annotations module. It is easy to register and if you already use Jackson for serialising/deserialising JSON this is obvious choice. How to do that you can find reading answers for this question on SO: Using JAXB with Google Android.
Pros:
You do not need to create new POJOs schema.
In case of changes in Restful API you use next version of entities-module project.
Cons:
Problems with enabling JAXB on Android.
Performance issues linked with using heavy JAXB and Jackson module layers.
New module
Second choice is to create new module with POJOs and use faster and smaller library like SimpleXML.
Pros:
Better performance
No problems with building app with depreciated JAXB classes.
You can optimise structure: ignore unused fields, choose better types, etc.
Cons:
You need to create new module with new classes structure.
Learn and maintain new library
In case of changes in API you need to duplicate changes in few modules.
You need to take a look on above pros and cons list and decide what is the best for you.
I would like to also add two more options:
Replace JAXB annotations with Jackson annotations. Jackson has really good XML support beside great for JSON. It will enable for you easy support for JSON and XML formats in your API. Also, you can use Jackson library on Android.
Create new JSON API for Android app. Mostly, UI flows on Android app is different than on Web App and you will, probably, end up with that idea anyway.
I have multi tier project with Model, DTO, Managers/Services using DTOs and also some CXF webservices clients with a layer that transforms responses to my models.
We are using different approaches, the project is big, we use for example Spring Beautils an many by-hand mappings which can produce errors, are hard to maintain but are fastest.
I've found Orika an interesting library, that doesnt use reflection int the way the Dozer does, it generates code (like the by-hand code) in some point but I don't know when - when you build your application with maven or when the application is being deployed on for example Weblogic server?
What would you chhose???
BTW: Do you know any other libraries that are worth using (I use Java 1.7, log4j, junit, spring, hibernate, cxf, jersey, guava, lamba4j)?
You could be interested to see JMapper Framework.
Especially as it permits to manage the multi-level mapping (wiki page here)
I've been searching the web but I didn't find anything that meet my requirements and am not sure what to do. I know this has been asked several times but not exactly the same as this.
We have some large XML files (still don't know size but I guess surely less than 1GB). We only need a part of this files (only a part of the XSD is useful for us), that we must read and then store in DB. In the future we'll probably need to recreate XML files, but this is not covered in this first phase.
Well, I've already seen that for something like this is better to use JAXB, but I'm a bit confused with JAXB implementations. We have JDK implementation, and Castor, and Metro, and EclipseLink Moxy, and I think I've seen at least 2 more implementations. Wich one would be the best to bind this XML to POJO classes and then to persist to DB with JPA? Is there a better implementation than the ones I've listed? Any of the ones I've listed is out-of-date? (I ask this because many pages I've been visiting are quite old and am not sure if there has been changes in the past years)
Performance is important, of course, but the important thing is that we only need part of the elements included in the XML. BTW, this is for use with SG1-XML standard.
Thanks in advance.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
JAXB (JSR-222) is the Java standard for XML binding it is leveraged by other standards such as JAX-WS (SOAP Web Services) and JAX-RS (RESTful Web Services).
Project JAXB (part of Metro) is the reference implementation, and the version of the JAXB included in most implementationjs of the JDK/JRE is derived from it.
EclipseLink MOXy is a JAXB compliant implementation passing all the necessary compliance tests. It offers useful extensions such as path based mapping and additional support for mapping JPA entities (EclipseLink also provides a JPA implementation).
Castor - Castor appears to offer atleast a partial JAXB implementation (see: http://docs.codehaus.org/display/CASTOR/Castor+JAXB). In general I would recommend staying away from anything that only implements part of a specification.
Since the document is large and you only need a portion of it, I would recommend using a JAXB implementation in combination with a StAX parser. You can use an XMLStreamReader to advance to the portion of the document you wish to unmarshal, and only unmarshal the chunk you need.
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
Which one would be the best to bind this XML to POJO classes and then
to persist to DB with JPA?
As MOXy is a component of EclipseLink which is the JPA reference implementation we spend a significant amount of effort on those use cases. I'm the MOXy lead and I share a cubicle wall with Mike Keith the former JPA co-spec lead.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
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:
javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller
XStream
XMLBean
JAXB
Castor
JIBX
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 ?
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).
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.
Some alternatives include:-
XStream
XMLBean
However, one significant advantage of going with JAXB is it ships with the Java distrubtion.
I know that this isn't what you asked, but have you considered JSON instead of XML? I'm currently working on a project that makes heavy use of JSON (with the Jackson library) and I'm very happy with it. Jackson works similarly to JAXB - it even works with JAXB annotations, in case you want to use both.
XMLBeans is also worth looking at.
Simple XML Serialization (simpleframework.org)
Another approach is Simple its far easier to use than JAXB, JiBX or other such tools. You simply annotate class and you can serialize any POJO to XML. Also, its probably the only framework that currently works on all known Java platforms, including Android, Google App Engine, any JDK 1.5+. For more information you can check out the Tutorial
There are also:
java.beans.XMLEncoder/ -Decoder
javax.xml.stream.XMLStreamWriter
But I recommend to use JAXB if you don't have a good reason to do otherwise.
You can look at castor and jibx as well.
Apache Digester is an option.Here is a tutorial for it.If JSON format is acceptable then you can use google-gson.
Castor allows generation of Java classes(decent implementations) straight from XSD schema (use case #1 above).
Just be sure, in Android 2.1, not to use default android SAXParser. You'll get namespace errors. You do this by defining the parser to be, for example, Xerces (and the you add the required JARS), in core.properties . In android 2.2 it may be ok. Note that if you create an xmlcontext for the unmarsheler with xerces, it still won't work, as the mapping itself would be parsed with android's SAX. It must be done at core (top level properties file) so that even the mapping is parsed by xerces. finally - performance is as slow as you can expect... :( Good luck SM
I use XFire to create a webservice wrapper around my application. XFire provides the webservice interface and WSDL at runtime (or creates them at compile time, don't know exactly).
Many of our customers don't know webservices very well and additionally they simply don't read any external documentation like Javadoc. I know that it's possible to add documentation (for parameters and methods) directly to the WSDL file.
I thought about Annotations or Aegis XML files but I don't know how... Do you know a way?
Edit: I just found this JIRA issue but the last activity was 2006. Any ideas?
XFire is slowly headed for /dev/null. Use CXF instead. In other words, XFire is being deprecated in favor of CXF - it's pretty much the same developers.
Since you use the Java-first approach, I suggest you generate you WSDL once and for all with CXF's java2wsdl tool, then you put your documentation in that and tell CXF to use that documented WSDL instead of letting CXF generate its own (undocumented) WSDL at runtime/deploy-time.
This page has pretty much everything you need to know about creating a web service in CXF.
And my last hint regarding CXF - use Aegis for data binding instead of JAXB. It doesn't work for complex POJO.
In my experience we have no problem with complex POJO and JAXB, the only problem is that the code starts to be cluttered with JAXBElements. On the other hand, Aegis documentation is pretty sparse and not well-maintained with relation to CXF.
Let's me add my two cents regarding XFire. We had very serious issue with XFie under JDK6 (both Tomcat 6.0 and 5.5).Please take a glance at that issue. In our case XFire with 4+ web services under JDK6 leads to hanging application servers (thread deadlocks etc.). It's interesting, but under JDK5 everything was OK.
And I completely agree with Christian Vest regarding migration to CXF from XFire. It has sense in many cases e.g. ESB Mule 2 doesn't support native XFire connectors anymore (see also).
And I wish to add that migration from XFire to CXF is not straitforward way (e.g. CXF has jar dependences that conflict with some hibernate releases see also), but it's doable. In our case we did it for couple days without code correction (Spring only configuration).
And my last hint regarding CXF - use Aegis for data binding instead of JAXB. It doesn't work for complex POJO.