如何将xml解析为java对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16364547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to parse xml to java object?
提问by C.c
I have a XML which is used to config some rules, it does not has complex structure, but this configuration is used anywhere in my system, so I want to parse this XML to java object and design as singleton mode, is any good way I can use it to unmarshal XML to Java object directly without write much codes?
我有一个 XML 用于配置一些规则,它没有复杂的结构,但是这个配置在我系统的任何地方都使用,所以我想将此 XML 解析为 java 对象并设计为单例模式,是什么好方法我可以使用它直接将 XML 解组为 Java 对象而无需编写太多代码吗?
I did some research on Google and known JAXB is a choice, my application is just some kinds of tool program which read rule and then follow do stuffs, JAXB could be used for web service more widely, it fit my projects?
我在Google上做了一些研究,知道JAXB是一种选择,我的应用程序只是某种读取规则然后执行操作的工具程序,JAXB可以更广泛地用于Web服务,它适合我的项目吗?
If yes, the most important question is, I used xjc to generate java object source class according xsd file, after unmarshal I will directly get these configurationType object, is it necessary I convert again, (from the JaxB classes to my owned java pojo object configuration), I see most coder did this, but why? because they are some data, just from the object generated to JAXB and copy to ourself created POJO object
如果是的话,最重要的问题是,我用xjc根据xsd文件生成java对象源类,unmarshal后我会直接得到这些configurationType对象,是否有必要我再转换一下,(从JaxB类到我拥有的java pojo对象配置),我看到大多数编码器都这样做了,但为什么呢?因为它们是一些数据,只是从生成的对象到 JAXB 并复制到我们自己创建的 POJO 对象
回答by Evgeniy Dorofeev
JAXB is an ideal solution. But you do not necessarily need xsd and xjc for that. More often than not you don't have an xsd but you know what your xml is. Simply analyze your xml, e.g.,
JAXB 是一个理想的解决方案。但是您不一定需要 xsd 和 xjc 。通常情况下,您没有 xsd,但您知道您的 xml 是什么。只需分析您的 xml,例如,
<customer id="100">
<age>29</age>
<name>mkyong</name>
</customer>
Create necessary model class(es):
创建必要的模型类:
@XmlRootElement
public class Customer {
String name;
int age;
int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
Try to unmarshal:
尝试解组:
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(new File("C:\file.xml"));
Check results, fix bugs!
检查结果,修复错误!
回答by Juned Ahsan
JAXB is a reliable choice as it does xml to java classes mapping smoothely. But there are other frameworks available, here is one such:
JAXB 是一个可靠的选择,因为它可以平滑地将 xml 映射到 java 类。但是还有其他可用的框架,这里是一个这样的:
回答by Soumya Sarkar
For performing Unmarshall using JAXB:
使用 JAXB 执行解组:
1) Convert given XML to XSD(by yourself or by online convertor),
1) 将给定的 XML 转换为 XSD(由您自己或通过在线转换器),
2) Create a JAXB project in eclipse,
2)在eclipse中创建一个JAXB项目,
3) Create XSD file and paste that converted XSD content in it,
3) 创建 XSD 文件并将转换后的 XSD 内容粘贴到其中,
4) Right click on **XSD file--> Generate--> JAXB Classes-->follow the instructions(this will create all nessasary .java files in src, i.e., one package-info, object factory and pojo class),
4) 右击**XSD 文件--> 生成--> JAXB 类--> 按照说明(这将在src 中创建所有nessasary .java 文件,即一个包信息、对象工厂和pojo 类),
5) Create another .java file in src to operate unmarshall operation, and run it.
5)在src中再创建一个.java文件进行unmarshall操作,并运行。
Happy Coding!!
快乐编码!!
回答by aks
One thing that is really important to understand considering you have an XML file as :
考虑到您有一个 XML 文件,理解这一点非常重要:
<customer id="100">
<Age>29</Age>
<NAME>mkyong</NAME>
</customer>
I am sorry to inform you but :
很抱歉通知您,但是:
@XmlElement
public void setAge(int age) {
this.age = age;
}
will not help you, as it tries to look for "age" instead of "Age" element name from the XML.
不会帮助你,因为它试图从 XML 中寻找“年龄”而不是“年龄”元素名称。
I encourage you to manually specify the element name matching the one in the XML file :
我鼓励您手动指定与 XML 文件中匹配的元素名称:
@XmlElement(name="Age")
public void setAge(int age) {
this.age = age;
}
And if you have for example :
如果你有例如:
@XmlRootElement
@XmlAccessorType (XmlAccessType.FIELD)
public class Customer {
...
It means it will use java beans by default, and at this time if you specify that you must not set another
表示默认会使用java bean,此时如果指定一定不要另外设置
@XmlElement(name="NAME")
@XmlElement(name="NAME")
annotation above a setter method for an element <NAME>..</NAME>
it will fail saying that there cannot be two elements on one single variables.
元素的 setter 方法上方的注释<NAME>..</NAME>
将失败,说明一个变量上不能有两个元素。
I hope that it helps.
我希望它有帮助。
回答by bluearrow
I find Hymanson fasterxml
is one good choice to serializing/deserializing
bean with XML.
我发现使用 XMLHymanson fasterxml
是一种不错的选择serializing/deserializing
。