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.
java xml jaxb xml-binding oxm
Waqas ali
source share