将 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 映射"(对象/XML 映射)或OXM".将 XML 文档转换为对象称为解组.
将对象转换(序列化)为 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.
注意:
术语编组和解组不仅与对象/XML相关,反之亦然.在此处阅读:编组(计算机科学).
NOTE:
The terms marshalling and unmarshalling relate NOT ONLY to Object/XML and vice versa transformation. Read here: Marshalling (Computer Science).
Java 自己的解决方案称为Java XML 绑定架构 (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例子 - 非常简单易学的教程
- JAXB 教程 – 获取开始 - 另一个带有示例的简短教程
- 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屋!