将XML映射到Java中的对象

时间:2021-02-13 21:51:04

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 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字符串并创建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 Test class object.

现在我这样做:我解析XML字符串并逐个获取每个节点的数据,然后调用setter方法为Test类对象的每个字段设置数据。

2 个解决方案

#1


11  

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映射”(对象/ XML映射)或“OXM”。将XML文档转换为对象称为解组。将对象转换(序列化)为XML文档称为编组。

NOTE:
The terms marshalling and unmarshalling relate NOT ONLY to Object/XML and vice versa transformation. Read here: Marshalling (Computer Science).

注意:术语编组和解组仅与Object / 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绑定的Java架构(JAXB)。它是JSR 222描述的规范.JDK包含JAXB实现,因此您(通常)不需要从JAXB Project主页下载独立的Reference Implementation(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

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 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.

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.
  • 基于XPath的映射 - 来自EclipseLink JAXB(MOXy)项目团队负责人Blaise Doughan的非常有用的文章。另请查看他博客中的其他文章。
  • Make your JAXB cleaner with the MOXy implementation - another helpful article with a nice example, outlining the advantage of EclipseLink MOXy.
  • 使用MOXy实现使您的JAXB更清洁 - 这是另一篇有用的文章,其中有一个很好的例子,概述了EclipseLink MOXy的优势。

#2


2  

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解析器。

#1


11  

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映射”(对象/ XML映射)或“OXM”。将XML文档转换为对象称为解组。将对象转换(序列化)为XML文档称为编组。

NOTE:
The terms marshalling and unmarshalling relate NOT ONLY to Object/XML and vice versa transformation. Read here: Marshalling (Computer Science).

注意:术语编组和解组仅与Object / 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绑定的Java架构(JAXB)。它是JSR 222描述的规范.JDK包含JAXB实现,因此您(通常)不需要从JAXB Project主页下载独立的Reference Implementation(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

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 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.

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.
  • 基于XPath的映射 - 来自EclipseLink JAXB(MOXy)项目团队负责人Blaise Doughan的非常有用的文章。另请查看他博客中的其他文章。
  • Make your JAXB cleaner with the MOXy implementation - another helpful article with a nice example, outlining the advantage of EclipseLink MOXy.
  • 使用MOXy实现使您的JAXB更清洁 - 这是另一篇有用的文章,其中有一个很好的例子,概述了EclipseLink MOXy的优势。

#2


2  

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解析器。