Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best practice in converting XML to Java object?

Tags:

java

object

xml

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?

like image 944
newbie Avatar asked May 12 '10 19:05

newbie


People also ask

What is the best way to parse XML in Java?

DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

How do you Unmarshal XML string to Java object using JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

Is XML compatible with Java?

XML provides a universal syntax for Java semantics (behavior). Simply put, this means that a developer can create descriptions for different types of data to make the data behave in various ways with Java programming code, and can later repeatedly use and modify those descriptions.


2 Answers

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

like image 177
James Kingsbery Avatar answered Sep 19 '22 03:09

James Kingsbery


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.

like image 33
mindas Avatar answered Sep 21 '22 03:09

mindas