I need to convert XML data to Java objects. What would be best practice to convert this XML data to object?
我需要将XML数据转换为Java对象。将此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. What would be best practice to handle this situation?
想法是通过Web服务获取数据(它不使用WSDL,只使用HTTP GET查询,因此我不能使用任何框架)并且答案是XML格式。处理这种情况的最佳做法是什么?
10 个解决方案
#1
10
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.
看看XStream。它可能不是最快的,但它是Java中用户友好且直接的转换器之一,特别是如果您的模型不复杂。
#2
12
JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
JAXB是执行此操作的标准API:http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
#3
4
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.
对于JMS项目,我们在TextMessages(字符串属性)中嵌入了编组和解组(从java到xml和xml到java)XML。我们尝试了JAXB,Jibx和XMLBeans。我们发现XMLBeans最适合我们。快速,易于配置,良好的文档和简单的Maven集成。
#4
3
I have used and will continue to use JDOM -> www.jdom.org
我已经使用并将继续使用JDOM - > www.jdom.org
#5
2
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.
另一个选择是Sax Parser。它是程序性的 - 即访问者模式 - 但如果xml相当轻量级(甚至中等重量),我发现它对此非常有用。
#6
2
JAXB API which comes in Java(In built).
Java中的JAXB API(内置)。
#7
1
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).
我在MQ模块中使用过JIBX。它工作得很好。 Ant配置很简单。使用Xsd2Jibx转换器从XML模式生成绑定文件和Java bean。编组和取消编组允许指定字符集参数。在我的项目中处理自定义字符集非常有用。但我在绑定编译器中发现了一个问题。如果Java bean具有更长的路径名,则它会生成具有更长文件名的类文件,这将导致Windows XP中出现问题(它具有最大文件长度限制)。
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.
我没有使用其他API。所以我不想与其他人比较。如果您决定使用JIBX,我希望这会有所帮助。
More details, please refer JIBX website
更多详情,请参阅JIBX网站
#8
0
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...
我也使用过XStream,它易于使用和定制。你可以添加自己的自定义转换器,这对我来说非常方便...
#9
0
So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!
所以很惊讶更多人没有提到Jibx。令人惊讶的lib和我认为使用比Jaxb更简单。性能也很好!
#10
0
For this you can also consider apache's bitwixt and simple framework for xml
为此,您还可以考虑apache的bitwixt和xml的简单框架
#1
10
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.
看看XStream。它可能不是最快的,但它是Java中用户友好且直接的转换器之一,特别是如果您的模型不复杂。
#2
12
JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
JAXB是执行此操作的标准API:http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
#3
4
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.
对于JMS项目,我们在TextMessages(字符串属性)中嵌入了编组和解组(从java到xml和xml到java)XML。我们尝试了JAXB,Jibx和XMLBeans。我们发现XMLBeans最适合我们。快速,易于配置,良好的文档和简单的Maven集成。
#4
3
I have used and will continue to use JDOM -> www.jdom.org
我已经使用并将继续使用JDOM - > www.jdom.org
#5
2
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.
另一个选择是Sax Parser。它是程序性的 - 即访问者模式 - 但如果xml相当轻量级(甚至中等重量),我发现它对此非常有用。
#6
2
JAXB API which comes in Java(In built).
Java中的JAXB API(内置)。
#7
1
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).
我在MQ模块中使用过JIBX。它工作得很好。 Ant配置很简单。使用Xsd2Jibx转换器从XML模式生成绑定文件和Java bean。编组和取消编组允许指定字符集参数。在我的项目中处理自定义字符集非常有用。但我在绑定编译器中发现了一个问题。如果Java bean具有更长的路径名,则它会生成具有更长文件名的类文件,这将导致Windows XP中出现问题(它具有最大文件长度限制)。
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.
我没有使用其他API。所以我不想与其他人比较。如果您决定使用JIBX,我希望这会有所帮助。
More details, please refer JIBX website
更多详情,请参阅JIBX网站
#8
0
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...
我也使用过XStream,它易于使用和定制。你可以添加自己的自定义转换器,这对我来说非常方便...
#9
0
So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!
所以很惊讶更多人没有提到Jibx。令人惊讶的lib和我认为使用比Jaxb更简单。性能也很好!
#10
0
For this you can also consider apache's bitwixt and simple framework for xml
为此,您还可以考虑apache的bitwixt和xml的简单框架