将 XML 映射到 Java 中的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16753945/
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
Mapping XML to an object in Java
提问by Waqas Ali
Suppose I have a class called Test, like this
假设我有一个名为Test的类,就像这样
public class Test {
private String testId;
private String description;
private String department;
public Test() {}
public Test(String id,String des,String dpt) {
this.testId = id;
this.department = dpt;
this.description = des;
}
public String getTestId() {
return testId;
}
public void setTestId(String testId) {
this.testId = testId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
Also an XML string that contains data for an object of the class Test. XML string is
也是一个 XML 字符串,其中包含类Test的对象的数据。XML 字符串是
<test>
<testId>1</testId>
<description>This is first test</description>
<department>surgeon</department>
</test>
Now my task is to parse that XML string and create an object of the class Testand put all of the data contained in this XML into that object. I am using JDOMfor XML parsing. I want to know is there any solution through which all of the data that is in the XML format is directly copied into Testobject?
现在我的任务是解析该 XML 字符串并创建Test类的对象,并将此 XML 中包含的所有数据放入该对象中。我正在使用JDOM进行 XML 解析。我想知道是否有任何解决方案可以将所有 XML 格式的数据直接复制到Test对象中?
Now I am doing this like this: I parse XML string and get data of every node one by one and then call setter method to set data for each field of the Testclass object.
现在我是这样做的:我解析XML字符串并一一获取每个节点的数据,然后调用setter方法为Test类对象的每个字段设置数据。
回答by informatik01
Short answer:Yes, there is such a solution.
简短回答:是的,有这样的解决方案。
It is called "XML data binding", or alternatively "O/X Mapping" (Object/XML Mapping), or "OXM".
Converting an XML document to an object is called unmarshalling.
Converting (serializing) an object to an XML document is called marshalling.
它被称为“ XML 数据绑定”,或者“ O/X 映射”(Object/XML Mapping),或者“ OXM”。将 XML 文档转换为对象称为解组。
将对象转换(序列化)为 XML 文档称为编组。
NOTE:
The terms marshallingand unmarshallingrelate NOT ONLY to Object/XML and vice versa transformation. Read here: Marshalling (Computer Science).
注:
该条款编组和反编组不仅涉及对象/ XML,反之亦然改造。在这里阅读:编组(计算机科学)。
Java's own solution is called Java Architecture for XML Binding (JAXB). It is a specification described by JSR 222. JDK includes a JAXB implementation, so you (usually) don't need to download standalone Reference Implementation (RI)from the JAXB Project home page.
Java 自己的解决方案称为Java Architecture for XML Binding (JAXB)。它是由JSR 222描述的规范。JDK 包含一个JAXB 实现,因此您(通常)不需要从JAXB 项目主页下载独立的参考实现 (RI)。
NOTE:
You can check, which version of JAXB your JDK has by using xjc (binding compiler), bundled with JDK: xjc -version
注意:
您可以使用与 JDK 捆绑的xjc(绑定编译器)来检查您的 JDK 具有哪个版本的 JAXB : xjc -version
Useful links
有用的链接
- Java Architecture for XML Binding- the official Oracle Java learning trail
- A JAXB Tutorial- the official tutorial from the JAXB project(Reference Implementation)
- JAXB hello world example- very simple and easy tutorial
- JAXB tutorial – Getting Started- another short tutorial with examples
- XML 绑定的 Java 体系结构- 官方 Oracle Java 学习路径
- A JAXB 教程- JAXB 项目的官方教程(参考实现)
- JAXB hello world 示例- 非常简单易学的教程
- JAXB 教程 - 入门- 另一个带有示例的简短教程
Just google "JAXB tutorial", there are tons of them.
只需谷歌“JAXB 教程”,就有很多。
Important note:
重要的提示:
JAXB is a specification and there are different implementations of it (including Reference Implementation). But these traditional implementations cannot use XPath, which is sad, because for the heavily nested XML files using XPath can be much more effective.
JAXB 是一种规范,它有不同的实现(包括参考实现)。但是这些传统的实现不能使用XPath,这是可悲的,因为使用 XPath 可以更有效地处理大量嵌套的 XML 文件。
EclipseLink MOXyprovides a JAXB implementation with many extensions. One of them is XPath based mapping. I found it extremely useful, when doing one of my projects, where OXM was involved.
EclipseLink MOXy提供了一个带有许多扩展的 JAXB 实现。其中之一是基于 XPath 的映射。我发现它在做我的一个项目时非常有用,其中涉及 OXM。
Here are some related links:
以下是一些相关链接:
- XPath Based Mapping- very helpful article from Blaise Doughan, the Team Lead for EclipseLink JAXB (MOXy) project. Also check out other articles in his blog.
- Make your JAXB cleaner with the MOXy implementation- another helpful article with a nice example, outlining the advantage of EclipseLink MOXy.
- 基于 XPath 的映射- 来自EclipseLink JAXB (MOXy) 项目团队负责人Blaise Doughan 的非常有用的文章。还可以查看他博客中的其他文章。
- 使用 MOXy 实现使您的 JAXB 更干净——另一篇有用的文章,其中包含一个很好的示例,概述了 EclipseLink MOXy 的优势。
回答by Petr Mensik
Use JAXB, it's a standard Java way how to XML bindings - that means converting objects to XML and back. You can just apply few annotations on your class and that's basically everything you need to do so you can avoid creation your own custom XML parser.
使用JAXB,它是如何进行 XML 绑定的标准 Java 方式 - 这意味着将对象转换为 XML 并返回。您只需在您的类上应用几个注释,这基本上就是您需要做的一切,这样您就可以避免创建自己的自定义 XML 解析器。