假设我有一个名为Test的类,就像这样
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;
}
}
还包含一个XML字符串,其中包含Test类对象的数据. XML字符串是
1
This is first test
surgeon
现在我的任务是解析该XML字符串并创建Test类的对象,并将此XML中包含的所有数据放入该对象中.我正在使用JDOM进行XML解析.我想知道是否有任何解决方案可以将XML格式的所有数据直接复制到Test对象中?
现在我这样做:我解析XML字符串并逐个获取每个节点的数据,然后调用setter方法为Test类对象的每个字段设置数据.