使用xstream实现 xml json 对象互转

http://ajava.org/course/open/13556.html

http://blog.myspace.cn/e/407329705.htm

 

HasSelf h = new HasSelf();
        h.setId(1);
        h.setName("aaa");
        HasSelf h2 = new HasSelf();
        h2.setId(2);
        h2.setName("bbb");
        h2.setHasSelf(h);
        h.setHasSelf(h2);
        XStream xstream = new XStream(new DomDriver());
        //配置节点别名
        xstream.alias("hasSelf", HasSelf.class);
        //过滤字段
        xstream.omitField(HasSelf.class, "hasSelf");
        String xml = xstream.toXML(h2);
        System.out.println(xml);

 

package com.clannan.model;

public class HasSelf {

    private int id;
    private String name;
    private HasSelf hasSelf;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public HasSelf getHasSelf() {
        return hasSelf;
    }
    public void setHasSelf(HasSelf hasSelf) {
        this.hasSelf = hasSelf;
    }
   
}

你可能感兴趣的:(json,xml,String,Class)