Mapping XML to an Object in Java

Suppose I have a Test class, for example

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 containing data for an object of class Test. XML string

 <test> <testId>1</testId> <description>This is first test</description> <department>surgeon</department> </test> 


Now my task is to parse this XML string and create an object of the Test class and put all the data contained in this XML into this object. I am using JDOM for XML parsing. I want to know if there is any solution through which all data in XML format is directly copied to the test object?

Now I do it this way: I parse the XML string and get the data from each node one by one, and then I call the setter method to set the data for each field of the object of the Test class.

+8
java xml jaxb xml-binding oxm
source share
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 into an object is called unmarshalling .
Converting (serializing) an object into an XML document is called sorting .

Note:
The terms marshalling and unmarshalling do NOT refer only to Object / XML and vice versa. Read here: Marshalling (computer science) .

The Java solution is called Java Architecture for XML Binding (JAXB) . This is the specification described by JSR 222 . The JDK includes a JAXB implementation , so you (usually) do not need to download a stand-alone reference implementation (RI) from the JAXB project home page .

Note:
You can check in which version of JAXB your JDK is using xjc (binding compiler) , complete with JDK: xjc -version


useful links

Just google β€œjaxb tutorial”, there are tons of them.


Important Note:

JAXB is a specification, and there are various implementations of it (including a reference implementation). But these traditional implementations cannot use XPath , which is sad because XPath can be much more efficient for heavily nested XML files.

EclipseLink MOXy provides a JAXB implementation with many extensions. One of them is XPath-based mapping . I found this extremely useful when I was doing one of my projects in which OXM was involved.

Here are some links:

+11
source share

Use JAXB , this is the standard way of Java as XML bindings - this means converting objects to XML and vice versa. You can simply apply a few annotations to your class, and that's basically all you need to do to avoid creating your own XML parser.

+2
source share

All Articles