我有一个想要序列化为XML请求的pojo类,我已经使用JAXB完成了这个,但是找不到一种序列化嵌套元素的方法。
pojo class
@XmlRootElement(name = "student")
public class StudentBean {
private int id;
private String firstName;
private String lastName;
private int age;
@XmlElement
public void setFirstName(String fname) {
this.firstName = fname;
}
public String getFirstName() {
return this.firstName;
}
// other getters and setter
我想要XML请求
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<id>123</id>
<name>
<firstName> fname </firstName>
<lastName> lname </lastName>
</name>
<age>12</age>
</student>
那么有什么方法可以像上面那样序列化xml请求? 谢谢!
答案 0 :(得分:1)
如果您查看XML并将每个直接子项视为输入对象,则学生有三个孩子:
XmlRootElement(name = "student")
public class StudentBean {
private int id;
private Name name;
private int age;
// getters & setters
您需要创建名称:
public class Name {
private String firstName;
private String lastName;
// getters & setters