Mapping XML to an object in Java

2019-02-03 19: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.

2条回答
疯言疯语
2楼-- · 2019-02-03 20:25

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-03 20:38

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

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:

查看更多
登录 后发表回答