xstream学习,对象和xml之间转换,对象和json之间转换

前端之家收集整理的这篇文章主要介绍了xstream学习,对象和xml之间转换,对象和json之间转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、测试对象

  1. package domain;
  2.  
  3. import java.util.Date;
  4.  
  5. public class User {
  6. private String name;
  7. private int age;
  8. private Date birthday;
  9. private Car car;
  10. private List<Book> books;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public Date getBirthday() {
  24. return birthday;
  25. }
  26. public void setBirthday(Date birthday) {
  27. this.birthday = birthday;
  28. }
  29. public Car getCar() {
  30. return car;
  31. }
  32. public void setCar(Car car) {
  33. this.car = car;
  34. }
  35. public List<Book> getBooks() {
  36. return books;
  37. }
  38. public void setBooks(List<Book> books) {
  39. this.books = books;
  40. }
  41. @Override
  42. public String toString() {
  43. return "User [name=" + name + ",age=" + age + ",birthday=" + birthday
  44. + ",car=" + "Car [brand=" + car.getBrand() + ",price=" + car.getPrice() + ",factory="
  45. + car.getFactory() + "]" + ",books=" + books + "]";
  46. }
  47.  
  48. }

  1. package domain;
  2.  
  3. public class Book {
  4.  
  5. private String bookName;
  6. private double price;
  7. private String author;
  8. public String getBookName() {
  9. return bookName;
  10. }
  11. public void setBookName(String bookName) {
  12. this.bookName = bookName;
  13. }
  14. public double getPrice() {
  15. return price;
  16. }
  17. public void setPrice(double price) {
  18. this.price = price;
  19. }
  20. public String getAuthor() {
  21. return author;
  22. }
  23. public void setAuthor(String author) {
  24. this.author = author;
  25. }
  26. @Override
  27. public String toString() {
  28. return "Book [bookName=" + bookName + ",price=" + price + ",author="
  29. + author + "]";
  30. }
  31. }

  1. package domain;
  2.  
  3. public class Car {
  4.  
  5. private String brand;
  6. private double price;
  7. private String factory;
  8. public String getBrand() {
  9. return brand;
  10. }
  11. public void setBrand(String brand) {
  12. this.brand = brand;
  13. }
  14. public double getPrice() {
  15. return price;
  16. }
  17. public void setPrice(double price) {
  18. this.price = price;
  19. }
  20. public String getFactory() {
  21. return factory;
  22. }
  23. public void setFactory(String factory) {
  24. this.factory = factory;
  25. }
  26. @Override
  27. public String toString() {
  28. return "Car [brand=" + brand + ",factory="
  29. + factory + "]";
  30. }
  31. }

2、对象到xml的转换
  1. package test;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class TestBean2XML {
  6.  
  7. //XStream 同时可以转换list和map
  8. public static void main(String[] args) {
  9. // map2XML();
  10. //------Car对象-----//
  11. Car car = new Car();
  12. car.setBrand("宝马");
  13. car.setFactory("上汽");
  14. car.setPrice(500000);
  15. //------Car对象-----//
  16. //------Book对象-----//
  17. Book book1 = new Book();
  18. book1.setBookName("语文");
  19. book1.setAuthor("张三");
  20. book1.setPrice(40.6);
  21. Book book2 = new Book();
  22. book2.setBookName("数学");
  23. book2.setAuthor("李四");
  24. book2.setPrice(23.5);
  25. Book book3 = new Book();
  26. book3.setBookName("英语");
  27. book3.setAuthor("王五");
  28. book3.setPrice(34);
  29. //------Book对象-----//
  30. //------User对象-----//
  31. User user = new User();
  32. user.setName("小红");
  33. user.setAge(12);
  34. user.setBirthday(new Date());
  35. user.setCar(car);
  36. List<Book> list = new ArrayList<Book>();
  37. list.add(book1);
  38. list.add(book2);
  39. list.add(book3);
  40. user.setBooks(list);
  41. //------User对象-----//
  42. //测试根据bean生成xml
  43. XStream xStream = new XStream();
  44. // xStream.alias("user",User.class);
  45. //将类重命名
  46. xStream.aliasType("用户",User.class);
  47. xStream.alias("book",Book.class);
  48. xStream.alias("car",Car.class);
  49. /*
  50. */
  51. //测试重命名属性
  52. /*
  53. xStream.aliasField("姓名",User.class,"name");
  54. xStream.aliasField("汽车价格",Car.class,"price");
  55. xStream.aliasField("作者",Book.class,"author");
  56. */
  57. //测试将字段申明在 xml的属性节点上,而非文本节点上,并重命名
  58. xStream.aliasAttribute(User.class,"name","姓名");
  59. xStream.aliasAttribute(Car.class,"price","汽车价格");
  60. xStream.aliasAttribute(Book.class,"author","作者");
  61. String xml = xStream.toXML(user);
  62. // System.out.println(xml);
  63. }
  64. //将map转换为xml
  65. public static void map2XML(){
  66. Map<String,Book> map = new HashMap<String,Book>();
  67. Book bean1 = new Book();
  68. bean1.setBookName("语文");
  69. bean1.setPrice(234);
  70. bean1.setAuthor("张三");
  71. Book bean2 = new Book();
  72. bean2.setBookName("语文");
  73. bean2.setPrice(123);
  74. bean2.setAuthor("李四");
  75. map.put("No.1",bean1);//put
  76. map.put("No.3",bean2);//put
  77. XStream xStream = new XStream();
  78. xStream.alias("book",Book.class);
  79. xStream.alias("key",String.class);
  80. xStream.useAttributeFor(Book.class,"bookName");
  81. String xml = xStream.toXML(map);
  82. System.out.println(xml);
  83. }
  84. }

3、xml到对象的转换
  1. package test;
  2.  
  3. import java.text.ParseException;
  4.  
  5. public class TestXML2Bean {
  6.  
  7. static String xml = "<user name = \"小红\">"+
  8. "<age>12</age>"+
  9. "<birthday>2015-07-14 14:33:42.567 UTC</birthday>"+
  10. "<car>"+
  11. "<brand>宝马</brand>"+
  12. "<price>500000.0</price>"+
  13. "<factory>上汽</factory>"+
  14. "</car>"+
  15. "<books>"+
  16. "<book>"+
  17. "<bookName>语文</bookName>"+
  18. "<price>40.6</price>"+
  19. "<author>张三</author>"+
  20. "</book>"+
  21. "<book>"+
  22. "<bookName>数学</bookName>"+
  23. "<price>23.5</price>"+
  24. "<author>李四</author>"+
  25. "</book>"+
  26. "<book>"+
  27. "<bookName>英语</bookName>"+
  28. "<price>34.0</price>"+
  29. "<author>王五</author>"+
  30. "</book>"+
  31. "</books>"+
  32. "</user>";
  33. public static void main(String[] args) {
  34. //这里一定要是用 DomDriver才能fromXML
  35. XStream xStream = new XStream(new DomDriver());
  36. xStream.alias("user",Car.class);
  37. xStream.registerConverter( new DateConverter());
  38. //如果是属性节点要同步设置xStream
  39. xStream.aliasAttribute(User.class,"name");
  40. User user = (User)xStream.fromXML(xml);
  41. System.out.println(user.toString());
  42. }
  43. }
  44. class DateConverter implements Converter {
  45. @Override
  46. public boolean canConvert(Class arg0) {
  47. return Date.class == arg0;
  48. }
  49. @Override
  50. public void marshal(Object arg0,HierarchicalStreamWriter arg1,MarshallingContext arg2) {
  51. }
  52. @Override
  53. public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext arg1) {
  54. GregorianCalendar calendar = new GregorianCalendar();
  55. SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
  56. try {
  57. calendar.setTime(dateFm.parse(reader.getValue()));
  58. } catch (ParseException e) {
  59. throw new ConversionException(e.getMessage(),e);
  60. }
  61. return calendar.getTime();
  62. }
  63. }

4、对象到json的转换
  1. package test;
  2.  
  3. import java.io.Writer;
  4.  
  5. public class TestBean2JSON {
  6.  
  7. public static void main(String[] args) {
  8. Car car = new Car();
  9. car.setBrand("宝马");
  10. car.setFactory("上汽");
  11. car.setPrice(500000);
  12. // ------Car对象-----//
  13.  
  14. // ------Book对象-----//
  15. Book book1 = new Book();
  16. book1.setBookName("语文");
  17. book1.setAuthor("张三");
  18. book1.setPrice(40.6);
  19. Book book2 = new Book();
  20. book2.setBookName("数学");
  21. book2.setAuthor("李四");
  22. book2.setPrice(23.5);
  23. Book book3 = new Book();
  24. book3.setBookName("英语");
  25. book3.setAuthor("王五");
  26. book3.setPrice(34);
  27. // ------Book对象-----//
  28.  
  29. // ------User对象-----//
  30. User user = new User();
  31. user.setName("小红");
  32. user.setAge(12);
  33. user.setBirthday(new Date());
  34. user.setCar(car);
  35. List<Book> list = new ArrayList<Book>();
  36. list.add(book1);
  37. list.add(book2);
  38. list.add(book3);
  39. user.setBooks(list);
  40. //使用JettisonMappedXmlDriver驱动转换
  41. /*
  42. XStream xstream = new XStream(new JettisonMappedXmlDriver());
  43. xstream.setMode(XStream.NO_REFERENCES);
  44. */
  45. //使用JsonHierarchicalStreamDriver驱动转换
  46. // 删除根节点
  47. XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
  48. public HierarchicalStreamWriter createWriter(Writer out) {
  49. return new JsonWriter(out,JsonWriter.DROP_ROOT_MODE);
  50. }
  51. });
  52. xstream.alias("user",User.class);
  53. xstream.alias("book",Book.class);
  54. xstream.alias("car",Car.class);
  55. String xml = xstream.toXML(user);
  56. System.out.println(xml);
  57. }
  58. }

5、json到对象的转换
  1. package test;
  2.  
  3. import com.thoughtworks.xstream.XStream;
  4. import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
  5.  
  6. import domain.Book;
  7. import domain.Car;
  8. import domain.User;
  9.  
  10. public class TestJSON2Bean {
  11.  
  12. static String json = "{\"user\": {"+
  13. "\"name\": \"小红\","+
  14. "\"age\": 12,"+
  15. "\"birthday\": \"2015-07-15 13:51:18.907 UTC\","+
  16. "\"car\": {"+
  17. "\"brand\": \"宝马\","+
  18. "\"price\": 500000.0,"+
  19. "\"factory\": \"上汽\""+
  20. "},"+
  21. "\"books\": ["+
  22. "{"+
  23. "\"bookName\": \"语文\","+
  24. "\"price\": 40.6,"+
  25. "\"author\": \"张三\""+
  26. "},"+
  27. "{"+
  28. "\"bookName\": \"数学\","+
  29. "\"price\": 23.5,"+
  30. "\"author\": \"李四\""+
  31. "},"+
  32. "{"+
  33. "\"bookName\": \"英语\","+
  34. "\"price\": 34.0,"+
  35. "\"author\": \"王五\""+
  36. "}"+
  37. "]"+
  38. "}}";
  39. static String json1 = "{\"books\": ["+
  40. "{"+
  41. "\"bookName\": \"语文\","+
  42. "\"price\": 40.6,"+
  43. "\"author\": \"张三\""+
  44. "},"+
  45. "{"+
  46. "\"bookName\": \"数学\","+
  47. "\"price\": 23.5,"+
  48. "\"author\": \"李四\""+
  49. "},"+
  50. "{"+
  51. "\"bookName\": \"英语\","+
  52. "\"price\": 34.0,"+
  53. "\"author\": \"王五\""+
  54. "}"+
  55. "]}";
  56. public static void main(String[] args) {
  57. System.out.println(json);
  58. XStream xstream = new XStream(new JettisonMappedXmlDriver());
  59. xstream.alias("user",Book.class);
  60. xstream.alias("car",Car.class);
  61. User user = (User)xstream.fromXML(json);
  62. //最后失败,貌似不能转 集合
  63. System.out.println(user.toString());
  64. }
  65. }

猜你在找的XML相关文章