问题标题 XStream序列化与反序列化 期望金额50金币 已托管0金币 提问人:任* 提问时间:2019-01-30
问题详情
特别要求
其他设置 期望完成时间:2019-02-06
管理员补充 收藏 我来回答
以下部分为回答内容,共1个回答
1楼

使用eclipse创建maven项目。

用maven管理jar包时在pom.xml中添加:

  1. <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->

  2. <dependency>  

  3. <groupId>com.thoughtworks.xstream</groupId>  

  4. <artifactId>xstream</artifactId>  

  5. <version>1.4.9</version>  

  6. </dependency>  

  7. <!-- https://mvnrepository.com/artifact/xpp3/xpp3_min -->  

  8. <dependency>  

  9. <groupId>xpp3</groupId>  

  10. <artifactId>xpp3_min</artifactId>  

  11. <version>1.1.4c</version>  

  12. </dependency>  

  13. <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->  

  14. <dependency>  

  15. <groupId>dom4j</groupId>  

  16. <artifactId>dom4j</artifactId>  

  17. <version>1.6.1</version>  

  18. </dependency>

  19. 创建实体类,

  20. package xp4;


  21. public class Address {  

  22.   

  23.     //国  

  24.     private String country;  

  25.     //省  

  26.     private String province;  

  27.     //市  

  28.     private String city;  

  29.     //县  

  30.     private String county;  

  31.     //镇  

  32.     private String town;

  33. public String getCountry() {

  34. return country;

  35. }

  36. public void setCountry(String country) {

  37. this.country = country;

  38. }

  39. public String getProvince() {

  40. return province;

  41. }

  42. public void setProvince(String province) {

  43. this.province = province;

  44. }

  45. public String getCity() {

  46. return city;

  47. }

  48. public void setCity(String city) {

  49. this.city = city;

  50. }

  51. public String getCounty() {

  52. return county;

  53. }

  54. public void setCounty(String county) {

  55. this.county = county;

  56. }

  57. public String getTown() {

  58. return town;

  59. }

  60. public void setTown(String town) {

  61. this.town = town;

  62. }  

  63.       

  64.       

  65. }


  66. package xp4;

  67.   

  68. import java.util.List;  

  69.   

  70. public class Person {  

  71.   

  72.     //姓名  

  73.     private String name;  

  74.     //性别  

  75.     private String sex;  

  76.     //年龄  

  77.     private int age;  

  78.     //地址  

  79.     private List<Address> Address;

  80. public String getName() {

  81. return name;

  82. }

  83. public void setName(String name) {

  84. this.name = name;

  85. }

  86. public String getSex() {

  87. return sex;

  88. }

  89. public void setSex(String sex) {

  90. this.sex = sex;

  91. }

  92. public int getAge() {

  93. return age;

  94. }

  95. public void setAge(int age) {

  96. this.age = age;

  97. }

  98. public List<Address> getAddress() {

  99. return Address;

  100. }

  101. public void setAddress(List<Address> address) {

  102. Address = address;

  103. }  

  104.       

  105.   



  106. }

  107. 工具类:

  108. package xp4;


  109. import com.thoughtworks.xstream.XStream;

  110. import com.thoughtworks.xstream.io.xml.DomDriver;


  111. /** 

  112.  * XStream工具类 

  113.  * @author sun.kai 

  114.  * 2016年8月13日 

  115.  */  

  116. public class XStreamUtil {  

  117.   

  118.     private static XStream xStream;  

  119.       

  120.     //JVM加载类时会执行这些静态的代码块,如果static代码块有多个,JVM将按照它们在类中出现的先后顺序依次执行它们,每个代码块只会被执行一次。  

  121.     static{  

  122.         xStream = new XStream(new DomDriver());  

  123.         /* 

  124.          * 使用xStream.alias(String name, Class Type)为任何一个自定义类创建到类到元素的别名 

  125.          * 如果不使用别名,则生成的标签名为类全名 

  126.          */  

  127.         xStream.alias("person", Person.class);  

  128.         xStream.alias("address", Address.class);  

  129.         //将某一个类的属性,作为xml头信息的属性,而不是子节点  

  130.        

...展开更多
......此处隐藏剩余部分1744字,需支付50金币方可查看...... 购买回答