tags:

views:

339

answers:

9

I need to convert XML data to Java objects. What would be best practice to convert this XML data to object?

Idea is to fetch data via a web service (it doesn't use WSDL, just HTTP GET queries, so I cannot use any framework) and answers are in XML. What would be best practice to handle this situation?

+10  A: 

JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

James Kingsbery
+2  A: 

Another option is a Sax Parser. It is procedural - i.e. a visitor pattern - but if the xml is fairly lightweight, (and even medium weight) I have found it to be very useful for this.

aperkins
+7  A: 

Have a look at XStream. It might not be the quickest, but it is one of the most user friendly and straightforward converters in Java, especially if your model is not complex.

mindas
+1 good answer, it's not a standard per-se, but it's been much more maintainable in my experience than JAXB.
maerics
+4  A: 

For a JMS project we were marshalling and unmarshalling (going from java to xml and xml to java) XML embedded in TextMessages (string property). We tried JAXB, Jibx, and XMLBeans. We found that XMLBeans worked best for us. Fast, easily configurable, good documentation, and easy Maven integration.

predhme
I've used JaxB, XStream, and XMLBeans, they've all got good and bad with them. Jaxb that its part of the platform, XStream that its faster than all get out, but if your XML is very complex, XMLBeans is the way to go.
mezmo
+3  A: 

I have used and will continue to use JDOM -> www.jdom.org

Jacob
A: 

I've used XStream as well, it is easy to use and customizable. You can add your own custom converters and that was very handy for me...

Salil
+1  A: 

I have used JIBX in MQ module. It works very well. Ant config is simple. Used Xsd2Jibx converter to generate the binding files and Java beans from XML schema. Marshalling and un-marshalling allow to specify character-set parameter. It was useful in my project to handle custom character-set. But I found an issue in the binding compiler. If the Java bean has lengthier path name, it generates class file with lengthier file name which will cause issue in Windows XP(it has a maximum file length limit).

I haven't used other APIs. So I am not trying to compare with others. If you decided to use JIBX, I hope this will be helpful.

More details, please refer JIBX website

Sujee
+1  A: 

JAXB API which comes in Java(In built).

Phani
A: 

So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!

Karl