xstream 使用方式

xml文件内容:

<list>
  <storagePool>
    <name>isopool</name>
    <path>/vms/isos</path>
    <type>dir</type>
    <totalSize>4983</totalSize>
    <freeSize>4810</freeSize>
    <status>1</status>
  </storagePool>
  <storagePool>
    <name>defaultpool</name>
    <path>/vms/images</path>
    <type>dir</type>
    <totalSize>4983</totalSize>
    <freeSize>4771</freeSize>
    <status>1</status>
  </storagePool>
</list>

 

 //测试代码

public static void main(String[] args) throws IOException {

        //由XML转化成JavaBean
        String filePath = "E:\\杂乱代码\\store.xml";
        String xml = FileUtil.readFileContent(filePath);
        System.out.println(xml);
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(H3CVMStoragePoolList.class);//必须把根目录注册进去
        H3CVMStoragePoolList h3CVMStoragePoolList = (H3CVMStoragePoolList) xstream.fromXML(xml);       
        for(H3CVMStoragePool temp : h3CVMStoragePoolList.getList())
        {
            System.out.println(temp.getName());
        }

 

       //由bean转化成xml的String

       H3CVolumAdd h3CVolumAdd = new H3CVolumAdd();
        h3CVolumAdd.setHostId(1124);
        h3CVolumAdd.setSpName("/df/df/df/df");
        h3CVolumAdd.setVolName("/fg/fg/fg");
        h3CVolumAdd.setBaseFile("/fgh/ghgfh");
        XStream xstream1 = new XStream(new DomDriver());

        //使用@XStreamOmitField对不生成xml的字段加注释
        //然后代码中使用xstream.autodetectAnnotations(true);就可以完成去掉属性了
        xstream1.autodetectAnnotations(true);
        String volAddXml = xstream1.toXML(h3CVolumAdd);
    
        System.out.println(volAddXml);

    }

 

@XStreamAlias("list")//这个注释必须有
public class H3CVMStoragePoolList implements Serializable{
    private static final long serialVersionUID = 1L;
   
    @XStreamImplicit(itemFieldName="storagePool")//这个注释必须有
    private ArrayList<H3CVMStoragePool> list;
    public ArrayList<H3CVMStoragePool> getList() {
        return list;
    }
    public void setList(ArrayList<H3CVMStoragePool> list) {
        this.list = list;
    }
}

 

@XStreamAlias("storagePool") //这个注释必须有
public class H3CVMStoragePool implements Serializable{
    private static final long serialVersionUID = 1L;
    String name;
    String path;
    String type;
    long totalSize;
    long freeSize;
    int status;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public long getTotalSize() {
        return totalSize;
    }
    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }
    public long getFreeSize() {
        return freeSize;
    }
    public void setFreeSize(long freeSize) {
        this.freeSize = freeSize;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
}

 

@XStreamAlias("volAddParameter")//这个注释信息必须有的
public class H3CVolumAdd {
    private long hostId;
    private String spName;
    private String  volName;
    private long capacity;
    private String format;
    //使用@XStreamOmitField对不生成xml的字段加注释
    //然后代码中使用xstream.autodetectAnnotations(true);就可以完成去掉属性了
    @XStreamOmitField
    private String baseFile;

    public long getHostId() {
        return hostId;
    }
    public void setHostId(long hostId) {
        this.hostId = hostId;
    }
    public String getSpName() {
        return spName;
    }
    public void setSpName(String spName) {
        this.spName = spName;
    }
    public String getVolName() {
        return volName;
    }
    public void setVolName(String volName) {
        this.volName = volName;
    }
    public long getCapacity() {
        return capacity;
    }
    public void setCapacity(long capacity) {
        this.capacity = capacity;
    }
    public String getFormat() {
        return format;
    }
    public void setFormat(String format) {
        this.format = format;
    }
    public String getBaseFile() {
        return baseFile;
    }
    public void setBaseFile(String baseFile) {
        this.baseFile = baseFile;
    }
}

你可能感兴趣的:(xstream 使用方式)