• XStream转换Java对象与XML


    1.引入需要的jar包,在pom.xml中配置依赖 

    Java代码  收藏代码
    1. <dependency>  
    2.     <groupId>com.thoughtworks.xstream</groupId>  
    3.     <artifactId>xstream</artifactId>  
    4.     <version>1.4.2</version>  
    5. </dependency>  


    2.创建要转换的实例类,本文以简单的硬件信息为例 

    Java代码  收藏代码
    1. package com.ajita.xstream;  
    2.   
    3. import com.thoughtworks.xstream.annotations.XStreamAlias;  
    4. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;  
    5.   
    6. @XStreamAlias("DiskInfo")  
    7. public class DiskInfo {  
    8.     @XStreamAsAttribute  
    9.     @XStreamAlias("diskName")  
    10.     private String name;  
    11.     private int totalSize;  
    12.     private int avilableSize;  
    13.     private int usedSize;  
    14.   
    15.     public String getName() {  
    16.         return name;  
    17.     }  
    18.   
    19.     public void setName(String name) {  
    20.         this.name = name;  
    21.     }  
    22.   
    23.     public int getTotleSize() {  
    24.         return totalSize;  
    25.     }  
    26.   
    27.     public void setTotleSize(int totleSize) {  
    28.         this.totalSize = totleSize;  
    29.     }  
    30.   
    31.     public int getAvilableSize() {  
    32.         return avilableSize;  
    33.     }  
    34.   
    35.     public void setAvilableSize(int avilableSize) {  
    36.         this.avilableSize = avilableSize;  
    37.     }  
    38.   
    39.     public int getUserSize() {  
    40.         return usedSize;  
    41.     }  
    42.   
    43.     public void setUserSize(int userSize) {  
    44.         this.usedSize = userSize;  
    45.     }  
    46.   
    47.     public String toString() {  
    48.         StringBuilder sb = new StringBuilder();  
    49.         sb.append(name + " ");  
    50.         sb.append("TotalSize:" + totalSize + "|Used Size:" + usedSize  
    51.                 + "|Available Size:" + avilableSize);  
    52.         return sb.toString();  
    53.     }  
    54. }  
    55.   
    56. package com.ajita.xstream;  
    57.   
    58. import java.util.List;  
    59.   
    60. import com.thoughtworks.xstream.annotations.XStreamAlias;  
    61.   
    62. @XStreamAlias("HardwareInfo")  
    63. public class HardwareInfo {  
    64.     private String cpuInfo;  
    65.     private String networkInfo;  
    66.     private List<DiskInfo> diskInfos;  
    67.   
    68.     public String getCpuInfo() {  
    69.         return cpuInfo;  
    70.     }  
    71.   
    72.     public void setCpuInfo(String cpuInfo) {  
    73.         this.cpuInfo = cpuInfo;  
    74.     }  
    75.   
    76.     public String getNetworkInfo() {  
    77.         return networkInfo;  
    78.     }  
    79.   
    80.     public void setNetworkInfo(String networkInfo) {  
    81.         this.networkInfo = networkInfo;  
    82.     }  
    83.   
    84.     public List<DiskInfo> getDiskInfos() {  
    85.         return diskInfos;  
    86.     }  
    87.   
    88.     public void setDiskInfos(List<DiskInfo> diskInfos) {  
    89.         this.diskInfos = diskInfos;  
    90.     }  
    91.   
    92.     public String toString() {  
    93.         StringBuilder sb = new StringBuilder();  
    94.         sb.append(cpuInfo);  
    95.         sb.append(" ");  
    96.         sb.append(networkInfo);  
    97.         sb.append(" ");  
    98.         for (DiskInfo disk : diskInfos) {  
    99.             sb.append(disk.toString());  
    100.             sb.append(" ");  
    101.         }  
    102.         return sb.toString();  
    103.     }  
    104. }  


    3.新建测试代码 

    Java代码  收藏代码
    1. package com.ajita.xstream;  
    2.   
    3. import java.util.ArrayList;  
    4.   
    5. import com.thoughtworks.xstream.XStream;  
    6.   
    7. public class TestXStream {  
    8.     /** 
    9.      * @param args 
    10.      */  
    11.     public static void main(String[] args) {  
    12.         DiskInfo disk1 = new DiskInfo();  
    13.         disk1.setName("D:\");  
    14.         disk1.setTotleSize(100);  
    15.         disk1.setUserSize(20);  
    16.         disk1.setAvilableSize(80);  
    17.         DiskInfo disk2 = new DiskInfo();  
    18.         disk2.setName("E:\");  
    19.         disk2.setTotleSize(200);  
    20.         disk2.setUserSize(40);  
    21.         disk2.setAvilableSize(160);  
    22.   
    23.         ArrayList<DiskInfo> diskInfos = new ArrayList<DiskInfo>();  
    24.         diskInfos.add(disk1);  
    25.         diskInfos.add(disk2);  
    26.   
    27.         HardwareInfo hwInfo = new HardwareInfo();  
    28.         hwInfo.setCpuInfo("cpu info is ...");  
    29.         hwInfo.setNetworkInfo("network info is ...");  
    30.         hwInfo.setDiskInfos(diskInfos);  
    31.   
    32.         XStream xStream = new XStream();  
    33.         xStream.autodetectAnnotations(true);  
    34.         String xml = xStream.toXML(hwInfo);  
    35.         System.out.println(xml);  
    36.   
    37.         HardwareInfo info2 = (HardwareInfo) xStream.fromXML(xml);  
    38.         System.out.println(info2);  
    39.     }  
    40.   
    41. }  


    输出如下: 

    Java代码  收藏代码
    1. <HardwareInfo>  
    2.   <cpuInfo>cpu info is ...</cpuInfo>  
    3.   <networkInfo>network info is ...</networkInfo>  
    4.   <diskInfos>  
    5.     <DiskInfo diskName="D:">  
    6.       <totalSize>100</totalSize>  
    7.       <avilableSize>80</avilableSize>  
    8.       <usedSize>20</usedSize>  
    9.     </DiskInfo>  
    10.     <DiskInfo diskName="E:">  
    11.       <totalSize>200</totalSize>  
    12.       <avilableSize>160</avilableSize>  
    13.       <usedSize>40</usedSize>  
    14.     </DiskInfo>  
    15.   </diskInfos>  
    16. </HardwareInfo>  
    17. cpu info is ...  
    18. network info is ...  
    19. D:  
    20. TotalSize:100|Used Size:20|Available Size:80  
    21. E:  
    22. 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.基金投资与策略(中低风险
    专题1. 投资理财概述与货币市场工具(低风险)
    七.风险防范与风险控制(风险分析)
    八.投资组合与估值
    五.固定收益类产品投资策略(投资分析)
    软考倒计时19天
    前三章
    软考倒计时21天:9大管理工具技术
    软考倒计时22天
    软考倒计时23天
  • 原文地址:https://www.cnblogs.com/soundcode/p/6595743.html
Copyright © 2020-2023  润新知