Mapping XML to an object in Java

℡╲_俬逩灬. 提交于 2019-11-29 01:00:47

问题


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;
    }

}


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>


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?

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.


回答1:


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.

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

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.

NOTE:
You can check, which version of JAXB your JDK has by using xjc (binding compiler), bundled with JDK: 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

Just google "JAXB tutorial", there are tons of them.


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.

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.

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.



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



来源:https://stackoverflow.com/questions/16753945/mapping-xml-to-an-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!