Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping XML to an object in Java

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.

like image 371
Waqas Ali Avatar asked May 25 '13 21:05

Waqas Ali


People also ask

What is XML map Java?

Introduction. Castor XML mapping is a way to simplify the binding of java classes to XML document. It allows to transform the data contained in a java object model into/from an XML document.

How do you Unmarshal XML string to Java object using JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

What is mapping object in Java?

A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys. A Map is useful if you have to search, update or delete elements on the basis of a key.


2 Answers

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:

  • 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.
like image 64
informatik01 Avatar answered Oct 12 '22 01:10

informatik01


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.

like image 32
Petr Mensik Avatar answered Oct 12 '22 03:10

Petr Mensik