首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Xstream施用示例【将对象序列化为XML和将XML反序列为对象】

2012-11-04 
Xstream使用示例【将对象序列化为XML和将XML反序列为对象】Xstream资源下载地址:http://xstream.codehaus.or

Xstream使用示例【将对象序列化为XML和将XML反序列为对象】

Xstream资源下载地址:http://xstream.codehaus.org/download.html

必须包:xstream-1.3.1.jar

测试程序

Person.java

?

Java代码??Xstream施用示例【将对象序列化为XML和将XML反序列为对象】
  1. <span?style="font-size:?large;">package?com.xstream.test;??
  2. ??
  3. public?class?Person?{??
  4. ????private?String?firstName;??
  5. ?????private?String?lastName;??
  6. ?????private?PhoneNumber?phonex;??
  7. ?????private?PhoneNumber?fax;??
  8. ?????public?Person(String?firstName,String?lastName){??
  9. ?????????this.firstName=firstName;??
  10. ?????????this.lastName=lastName;??
  11. ?????}??
  12. ???????
  13. ????public?String?getFirstName()?{??
  14. ????????return?firstName;??
  15. ????}??
  16. ????public?void?setFirstName(String?firstName)?{??
  17. ????????this.firstName?=?firstName;??
  18. ????}??
  19. ????public?String?getLastName()?{??
  20. ????????return?lastName;??
  21. ????}??
  22. ????public?void?setLastName(String?lastName)?{??
  23. ????????this.lastName?=?lastName;??
  24. ????}??
  25. ????public?PhoneNumber?getPhonex()?{??
  26. ????????return?phonex;??
  27. ????}??
  28. ????public?void?setPhonex(PhoneNumber?phonex)?{??
  29. ????????this.phonex?=?phonex;??
  30. ????}??
  31. ????public?PhoneNumber?getFax()?{??
  32. ????????return?fax;??
  33. ????}??
  34. ????public?void?setFax(PhoneNumber?fax)?{??
  35. ????????this.fax?=?fax;??
  36. ????}??
  37. ???????
  38. }??
  39. </span>??

?

?PhoneNumber.java

?

Java代码??Xstream施用示例【将对象序列化为XML和将XML反序列为对象】
  1. <span?style="font-size:?large;">package?com.xstream.test;??
  2. ??
  3. public?class?PhoneNumber?{??
  4. ??
  5. ????private?int?code;??
  6. ????private?int?number;??
  7. ??
  8. ????public?PhoneNumber(int?code,?int?number)?{??
  9. ????????this.code?=?code;??
  10. ????????this.number?=?number;??
  11. ????}??
  12. ??
  13. ????public?int?getCode()?{??
  14. ????????return?code;??
  15. ????}??
  16. ??
  17. ????public?void?setCode(int?code)?{??
  18. ????????this.code?=?code;??
  19. ????}??
  20. ??
  21. ????public?int?getNumber()?{??
  22. ????????return?number;??
  23. ????}??
  24. ??
  25. ????public?void?setNumber(int?number)?{??
  26. ????????this.number?=?number;??
  27. ????}??
  28. ??
  29. }??
  30. </span>??

?XstreamTest.java

?

Java代码??Xstream施用示例【将对象序列化为XML和将XML反序列为对象】
  1. <span?style="font-size:?large;">package?com.xstream.test;??
  2. ??
  3. import?com.thoughtworks.xstream.XStream;??
  4. import?com.thoughtworks.xstream.io.xml.DomDriver;??
  5. ??
  6. public?class?XstreamTest?{??
  7. ??
  8. ????/**?
  9. ?????*??
  10. ?????*?使用xStream.alias(String?elementName,?Class?cls)为任何一个自定义类创建到类到元素的别名;?
  11. ?????*?使用xStream.toXML(Object?obj)转换对象到XML;?
  12. ?????*?使用xStream.fromXML(String?xml)转换XML到对象;?
  13. ?????*?@param?args?
  14. ?????*/??
  15. ????public?static?void?main(String[]?args)?{??
  16. ????????XStream?xStream?=?new?XStream(new?DomDriver());??
  17. ????????Person?joe?=?new?Person("Joe","Walnes");???
  18. ????????joe.setPhonex(new?PhoneNumber(123,222));???
  19. ????????joe.setFax(new?PhoneNumber(123,444));???
  20. ??????????
  21. ????????//这是可选的一步。没有这步XStream也可以很好的起作用,但是XML元素的名字就会包含??
  22. ????????//每个类的全称(包括包名),这将会使生成XML稍大。??
  23. ????????xStream.alias("person",Person.class);???
  24. ????????//序列化为XML??
  25. ????????String?xml=xStream.toXML(joe);??
  26. ????????System.out.println("对象序列化为XML:\n"+xml);??
  27. ????????//反序列化为对象??
  28. ????????Person?newJoe?=?(Person)xStream.fromXML(xml);???
  29. ????????System.out.println("XML反序列化为对象:\n"+newJoe);??
  30. ????}??
  31. ??????
  32. ??
  33. ??
  34. }??
  35. </span> ?

热点排行