简体   繁体   English

将XML转换为Java对象的最佳做法是什么?

[英]What is best practice in converting XML to Java object?

I need to convert XML data to Java objects. 我需要将XML数据转换为Java对象。 What would be best practice to convert this XML data to object? 将此XML数据转换为对象的最佳做法是什么?

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. 想法是通过Web服务获取数据(它不使用WSDL,只使用HTTP GET查询,因此我不能使用任何框架)并且答案是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 . 看看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. 它可能不是最快的,但它是Java中用户友好且直接的转换器之一,特别是如果您的模型不复杂。

For a JMS project we were marshalling and unmarshalling (going from java to xml and xml to java) XML embedded in TextMessages (string property). 对于JMS项目,我们在TextMessages(字符串属性)中嵌入了编组和解组(从java到xml和xml到java)XML。 We tried JAXB, Jibx , and XMLBeans . 我们尝试了JAXB, JibxXMLBeans We found that XMLBeans worked best for us. 我们发现XMLBeans最适合我们。 Fast, easily configurable, good documentation, and easy Maven integration. 快速,易于配置,良好的文档和简单的Maven集成。

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

Another option is a Sax Parser. 另一种选择是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. 它是程序性的 - 即访问者模式 - 但如果xml相当轻量级(甚至中等重量),我发现它对此非常有用。

Java中的JAXB API(内置)。

I have used JIBX in MQ module. 我在MQ模块中使用过JIBX。 It works very well. 它工作得很好。 Ant config is simple. Ant配置很简单。 Used Xsd2Jibx converter to generate the binding files and Java beans from XML schema. 使用Xsd2Jibx转换器从XML模式生成绑定文件和Java bean。 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). 如果Java bean具有较长的路径名,则它会生成具有较长文件名的类文件,这将导致Windows XP中出现问题(它具有最大文件长度限制)。

I haven't used other APIs. 我没有使用其他API。 So I am not trying to compare with others. 所以我不想与其他人比较。 If you decided to use JIBX, I hope this will be helpful. 如果您决定使用JIBX,我希望这会有所帮助。

More details, please refer JIBX website 更多细节请参考JIBX网站

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

I've used XStream as well, it is easy to use and customizable. 我也使用过XStream,它易于使用和定制。 You can add your own custom converters and that was very handy for me... 你可以添加自己的自定义转换器,这对我来说非常方便...

So surprised more people have not mentioned Jibx. 所以很惊讶更多人没有提到Jibx。 Amazing lib and i think a lot simpler to use than Jaxb. 令人惊讶的lib和我认为使用比Jaxb更简单。 Performance is also fab! 性能也很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Java 枚举序列化为 XML 的最佳做法是什么? - What is best practice for serializing Java enums to XML? Java中KV对象的最佳实践是什么? - What is the best practice for KV object in java? 最佳实践:Java / XML序列化:如何确定要反序列化的类? - Best practice: Java/XML serialization: how to determine to what class to deserialize? 在 JAVA 中处理静态 Web 服务中的复杂对象的最佳实践是什么 - what is the best practice for handle complex object in a restful web services in JAVA 在java中的方法中在运行时初始化对象的最佳实践是什么? - What is a best practice to initialize an object at runtime in a method in java? Java对象初始化的最佳实践 - Best practice for java object initialization 将Java对象转换为XML? - Converting java object into XML? 在Java中将List <Long>对象转换为long []数组的最佳方法是什么? - What is the best way of converting List<Long> object to long[] array in java? 从Java到XML的序列化/反序列化的最佳实践 - Best Practice for Serialize/Deserialize from Java to XML 在Java中将对象转换为String的接受实践? - Accepted practice for converting an Object to and from a String in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM