手记

XStream转换Java对象与XML

1.引入需要的jar包,在pom.xml中配置依赖
Java代码 收藏代码
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.2</version>
</dependency>

2.创建要转换的实例类,本文以简单的硬件信息为例
Java代码 收藏代码
package com.ajita.xstream;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("DiskInfo")
public class DiskInfo {
@XStreamAsAttribute
@XStreamAlias("diskName")
private String name;
private int totalSize;
private int avilableSize;
private int usedSize;

public String getName() {  
    return name;  
}  

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

public int getTotleSize() {  
    return totalSize;  
}  

public void setTotleSize(int totleSize) {  
    this.totalSize = totleSize;  
}  

public int getAvilableSize() {  
    return avilableSize;  
}  

public void setAvilableSize(int avilableSize) {  
    this.avilableSize = avilableSize;  
}  

public int getUserSize() {  
    return usedSize;  
}  

public void setUserSize(int userSize) {  
    this.usedSize = userSize;  
}  

public String toString() {  
    StringBuilder sb = new StringBuilder();  
    sb.append(name + "\r\n");  
    sb.append("TotalSize:" + totalSize + "|Used Size:" + usedSize  
            + "|Available Size:" + avilableSize);  
    return sb.toString();  
}  

}

package com.ajita.xstream;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("HardwareInfo")
public class HardwareInfo {
private String cpuInfo;
private String networkInfo;
private List<DiskInfo> diskInfos;

public String getCpuInfo() {  
    return cpuInfo;  
}  

public void setCpuInfo(String cpuInfo) {  
    this.cpuInfo = cpuInfo;  
}  

public String getNetworkInfo() {  
    return networkInfo;  
}  

public void setNetworkInfo(String networkInfo) {  
    this.networkInfo = networkInfo;  
}  

public List<DiskInfo> getDiskInfos() {  
    return diskInfos;  
}  

public void setDiskInfos(List<DiskInfo> diskInfos) {  
    this.diskInfos = diskInfos;  
}  

public String toString() {  
    StringBuilder sb = new StringBuilder();  
    sb.append(cpuInfo);  
    sb.append("\r\n");  
    sb.append(networkInfo);  
    sb.append("\r\n");  
    for (DiskInfo disk : diskInfos) {  
        sb.append(disk.toString());  
        sb.append("\r\n");  
    }  
    return sb.toString();  
}  

}

3.新建测试代码
Java代码 收藏代码
package com.ajita.xstream;

import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;

public class TestXStream {
/**

  • @param args
    */
    public static void main(String[] args) {
    DiskInfo disk1 = new DiskInfo();
    disk1.setName("D:\");
    disk1.setTotleSize(100);
    disk1.setUserSize(20);
    disk1.setAvilableSize(80);
    DiskInfo disk2 = new DiskInfo();
    disk2.setName("E:\");
    disk2.setTotleSize(200);
    disk2.setUserSize(40);
    disk2.setAvilableSize(160);

    ArrayList<DiskInfo> diskInfos = new ArrayList<DiskInfo>();  
    diskInfos.add(disk1);  
    diskInfos.add(disk2);  
    
    HardwareInfo hwInfo = new HardwareInfo();  
    hwInfo.setCpuInfo("cpu info is ...");  
    hwInfo.setNetworkInfo("network info is ...");  
    hwInfo.setDiskInfos(diskInfos);  
    
    XStream xStream = new XStream();  
    xStream.autodetectAnnotations(true);  
    String xml = xStream.toXML(hwInfo);  
    System.out.println(xml);  
    
    HardwareInfo info2 = (HardwareInfo) xStream.fromXML(xml);  
    System.out.println(info2);  

    }

}

输出如下:
Java代码 收藏代码
<HardwareInfo>
<cpuInfo>cpu info is ...</cpuInfo>
<networkInfo>network info is ...</networkInfo>
<diskInfos>
<DiskInfo diskName="D:\">
<totalSize>100</totalSize>
<avilableSize>80</avilableSize>
<usedSize>20</usedSize>
</DiskInfo>
<DiskInfo diskName="E:\">
<totalSize>200</totalSize>
<avilableSize>160</avilableSize>
<usedSize>40</usedSize>
</DiskInfo>
</diskInfos>
</HardwareInfo>
cpu info is ...
network info is ...
D:\
TotalSize:100|Used Size:20|Available Size:80
E:\
TotalSize:200|Used Size:40|Available Size:160

4.常用XStream注解
1)@XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("blog", Blog.class);
2)@XStreamImplicit去集合节点名:相当于 xstream.addImplicitCollection(Blog.class, "entries");
3) @XStreamConverter(SingleValueCalendarConverter.class)
private Calendar createdTime = new GregorianCalendar();

3人推荐
随时随地看视频
慕课网APP