将XML映射到Java中的对象 [英] Mapping XML to an object in Java
问题描述
假设我有一个名为 Test 的类,就像这个
Suppose I have a class called Test, like this
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;
}
}
也是一个XML字符串,其中包含类 Test 的对象的数据。 XML字符串是
Also an XML string that contains data for an object of the class Test. XML string is
<test>
<testId>1</testId>
<description>This is first test</description>
<department>surgeon</department>
</test>
现在我的任务是解析该XML字符串并创建一个对象类 Test ,并将此XML中包含的所有数据放入该对象中。我正在使用 JDOM 进行XML解析。我想知道是否有任何解决方案可以将XML格式的所有数据直接复制到 Test 对象中?
Now my task is to parse that XML string and create an object of the class Test and put all of the data contained in this XML into that object. I am using JDOM for 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 Test object?
现在我是这样做的:我解析XML字符串并逐个获取每个节点的数据,然后调用setter方法为 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 Test class object.
推荐答案
简答:是的,有这样的解决方案。
Short answer: Yes, there is such a solution.
它被称为 XML数据绑定,或者 O / X Mapping (对象/ XML映射)或 OXM 。
将XML文档转换为对象称为 unmarshalling 。
将对象转换(序列化)为XML文档称为编组。
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.
注意:
marshalling 和 unmarshalling 这两个术语不仅仅与Object / XML相关反之亦然。请阅读此处:编组(计算机科学)。
NOTE:
The terms marshalling and unmarshalling relate NOT ONLY to Object/XML and vice versa transformation. Read here: Marshalling (Computer Science).
Java自己的解决方案称为 用于XML绑定的Java体系结构(JAXB) 强> 。它是由 JSR 222 描述的规范。 JDK包括 JAXB实施,所以你(通常)不需要从 JAXB下载独立的参考实现(RI)项目主页。
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.
注意:
您可以使用 xjc(绑定编译器) ,与JDK捆绑在一起: xjc -version
- 用于XML的Java架构
绑定 -
官方Oracle Java学习路径 - JAXB教程 - 官方教程来自 JAXB项目的
(参考
实施) - JAXB hello world
示例 - 非常简单易学的b $ b $ liJAXB教程 - 获得
开始 - 另一个带示例的简短教程
- 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
只是谷歌JAXB教程,有很多。
Just google "JAXB tutorial", there are tons of them.
JAXB是一个规范,它有不同的实现(包括参考实现)。但是这些传统的实现无法使用 XPath ,这很难过,因为对于使用大量嵌套的XML文件XPath可以更加有效。
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.
EclipseLink MOXy 提供了许多扩展的JAXB实现。其中之一是基于XPath的映射。我发现在进行OXM项目时,我发现它非常有用。
EclipseLink MOXy provides 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.
以下是一些相关链接:
- 基于XPath
映射 -
来自 Blaise Doughan 的非常有用的文章,团队负责人
用于EclipseLink JAXB(MOXy)项目。另请查看他的博客
中的其他文章。 - 使用MOXy实现使您的JAXB更清洁 - 另一篇有用的文章,其中有一个很好的例子,概述了EclipseLink MOXy的优势。
- 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.
这篇关于将XML映射到Java中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!