xml與java對象轉換 -- XStreamAlias

 

@XStreamAlias使用java

1、 特色:ide

簡化的API; 
無映射文件; 
高性能,低內存佔用; 
整潔的XML; 
不須要修改對象;支持內部私有字段,不須要setter/getter方法 
提供序列化接口; 
自定義轉換類型策略; 
工具


XStream的優勢不少,可是也有一些小bug,好比在定義別名中的下劃線「_」轉換爲xml後會變成「__」這個符號, 不過下劃線問題能夠用下面這個解決
性能

 XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));單元測試

2、 使用場景測試

Transport轉換 
Persistence 持久化對象 
Configuration 配置 
Unit Tests 單元測試
ui

Interface服務之間XML消息的交互this

 

案例代碼:spa

@XStreamAlias("kfpt_dept") //注意這個,後面變成了<kfpt__dept> 下劃線。。。。 class Dept {
    private String deptName;

    private List<Staff> staffs;

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public List<Staff> getStaffs() {
        return staffs;
    }

    public void setStaffs(List<Staff> staffs) {
        this.staffs = staffs;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}

@XStreamAlias("kfpt_staffs")
class Staff {
    private String name;
    private int age;

    public Staff() {
        super();
    }

    public Staff(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

}

 

轉換工具類:code

public class XstreamUtil {
    
    /**
     * 將bean轉換爲xml
     * @param obj 轉換的bean
     * @return bean轉換爲xml
     */
    public static String objectToXml(Object obj) {
      //  XStream xStream = new XStream();
 XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));//解決下劃線問題
//xstream使用註解轉換 xStream.processAnnotations(obj.getClass()); return xStream.toXML(obj); } /** * 將xml轉換爲bean * @param <T> 泛型 * @param xml 要轉換爲bean的xml * @param cls bean對應的Class * @return xml轉換爲bean */ public static <T> T xmlToObject(String xml, Class<T> cls){ XStream xstream = new XStream(new DomDriver()); //xstream使用註解轉換 xstream.processAnnotations(cls); return (T) xstream.fromXML(xml); } }

 

main方法測試

public static void main(String[] args) {
        //java bean 轉 xml
        Dept d = new Dept();
        List<Staff> staffs = new ArrayList<>();
        Staff s1 = new Staff("wuyun", 20);
        Staff s2 = new Staff("lilei", 22);
        staffs.add(s1);
        staffs.add(s2);
        d.setDeptName("開放平臺");
        d.setStaffs(staffs);
        System.out.println(objectToXml(d));
/*
 用這個方法 XStream xStream = new XStream(); 會有下劃線問題

<kfpt__dept>
<deptName>開放平臺</deptName>
<staffs>
<kfpt__staffs>
<name>wuyun</name>
<age>20</age>
</kfpt__staffs>
<kfpt__staffs>
<name>lilei</name>
<age>22</age>
</kfpt__staffs>
</staffs>
</kfpt__dept>

 

XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));//能夠解決下劃線問題

<kfpt_dept>
<deptName>開放平臺</deptName>
<staffs>
<kfpt_staffs>
<name>wuyun</name>
<age>20</age>
</kfpt_staffs>
<kfpt_staffs>
<name>lilei</name>
<age>22</age>
</kfpt_staffs>
</staffs>
</kfpt_dept>

*/
        
        //xml 轉 java bean 
        // String xml = "<kfpt__dept><deptName>開放平臺</deptName><staffs><kfpt__staffs><name>wuyun</name><age>20</age></kfpt__staffs><kfpt__staffs><name>lilei</name><age>22</age></kfpt__staffs></staffs></kfpt__dept>";
        //        XStream xstream = new XStream(new DomDriver());
        //        //xstream使用註解轉換
        //        xstream.processAnnotations(Dept.class);
        //        System.out.println(((Dept) xstream.fromXML(xml)).toString());

        //        System.out.println(xmlToObject(xml, Dept.class).toString());
/*

com.cmcc.open.ss.vo.req.Dept@5660d2d2[
deptName=開放平臺
staffs=[com.cmcc.open.ss.vo.req.Staff@6eb1054b[
name=wuyun
age=20
], com.cmcc.open.ss.vo.req.Staff@27d2b7b3[
name=lilei
age=22
]]
]

*/
    }
相關文章
相關標籤/搜索