简体   繁体   中英

What is best practice in converting XML to Java object?

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?

JAXB是执行此操作的标准API: http//java.sun.com/developer/technicalArticles/WebServices/jaxb/

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.

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.

我已经使用并将继续使用JDOM - > www.jdom.org

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

Java中的JAXB API(内置)。

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

为此,您还可以考虑apache的bitwixt和xml的简单框架

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM