将 XML 映射到 Java 中的对象 [英] Mapping XML to an object in Java

查看:31
本文介绍了将 XML 映射到 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

只需谷歌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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆