<Student Name="张三"> <age>19</age> </Student>
输出如上xml格式:创建一个pojo类-->Student
package mytest;
public class Student
{
/** 姓名 */
private String name;
private Integer age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Integer getAge()
{
return age;
}
public void setAge(Integer age)
{
this.age = age;
}
}
测试方法 :
public void test1()
{
XStream stream = new XStream();
stream.alias("Student", Student.class);
stream.aliasAttribute(Student.class, "name", "Name");// 首先需要声明属性,然后才能当作属性调用,参数alias:显示的XML值
stream.useAttributeFor(Student.class, "name");// 参数fieldName需要对应aliasAttribute方法中的attributeName
Student stu = new Student();
stu.setName("张三");
stu.setAge(19);
System.out.println(stream.toXML(stu));
}