首页 > 代码库 > 利用Xstream注解生成和解析xml

利用Xstream注解生成和解析xml

实体类:

@XStreamAlias("person")
public class PersonBean {
    @XStreamAlias("firstName")
    private String firstName;
    @XStreamAlias("lastName")
    private String lastName;   
    @XStreamAlias("telphone")
    private PhoneNumber tel;
    @XStreamAlias("faxphone")
    private PhoneNumber fax;   
    //测试一个标签下有多个同名标签
    @XStreamAlias("friends")
    private Friends friend;  
    //测试一个标签下循环对象
    @XStreamAlias("pets")
    private Pets pet;
    //省略setter和getter
}
 
工具类:

public class XMLUtils {
public static String toXml(Object obj){
  XStream xstream=new XStream();
  xstream.processAnnotations(obj.getClass());
  return xstream.toXML(obj);
 }

}