当前位置: 首页 > news >正文

XStream(xml/bean转换)

XStream

1. 什么作用
  * 可以把JavaBean转换为(序列化为)xml

2. XStream的jar包
  * 核心JAR包:xstream-1.4.7.jar;
  * 必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);
3. 使用步骤
  * XStream xstream = new XStream();
  * String xmlStr = xstream.toXML(javabean);
4. 使用细节
  * 别名:把类型对应的元素名修改了
    > xstream.alias("china", List.class):让List类型生成的元素名为china
    > xstream.alias("province", Province.class):让Province类型生成的元素名为province
  * 使用为属性:默认类的成员,生成的是元素的子元素!我们希望让类的成员生成元素的属性
    > xstream.useAttributeFor(Province.class, "name"):把Province类的名为name成员,生成<province>元素的name属性
  * 去除Collection类型的成名:我们只需要Collection的内容,而不希望Collection本身也生成一个元素
    > xstream.addImplicitCollection(Province.class, "cities"):让Province类的名为cities(它是List类型的,它的内容还会生成元素)的成名不生成元素
  * 去除类的指定成名,让其不生成xml元素
    > xstream.omitField(City.class, "description"):在生成的xml中不会出现City类的名为description的对应的元素!

具体实例

[java]  view plain  copy
 
  1. package cn.itcast.demo1;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.junit.Test;  
  7.   
  8. import com.thoughtworks.xstream.XStream;  
  9.   
  10. /** 
  11.  * 演示XStream 
  12.  * @author cxf 
  13.  * 
  14.  */  
  15. public class Demo1 {  
  16.     // 返回javabean集合  
  17.     public List<Province> getProinvceList() {  
  18.         Province p1 = new Province();  
  19.         p1.setName("北京");  
  20.         p1.addCity(new City("东城区", "DongChengQu"));  
  21.         p1.addCity(new City("昌平区", "ChangPingQu"));  
  22.           
  23.         Province p2 = new Province();  
  24.         p2.setName("辽宁");  
  25.         p2.addCity(new City("沈阳", "shenYang"));  
  26.         p2.addCity(new City("葫芦岛", "huLuDao"));  
  27.           
  28.         List<Province> provinceList = new ArrayList<Province>();  
  29.         provinceList.add(p1);  
  30.         provinceList.add(p2);  
  31.           
  32.         return provinceList;  
  33.     }  
  34.       
  35.     /** 
  36. <list> --> List类型显示list 
  37.   <cn.itcast.demo1.Province> --> javabean的类型为Province,它元素的名称为类的完整名 
  38.     <name>北京</name> --> javabean的属性名 
  39.     <cities> --> javabean的属性名 
  40.       <cn.itcast.demo1.City> --> 类名 
  41.         <name>东城区</name> --> 属性名 
  42.         <description>DongChengQu</description> --> 属性名 
  43.       </cn.itcast.demo1.City> 
  44.       <cn.itcast.demo1.City> 
  45.         <name>昌平区</name> 
  46.         <description>ChangPingQu</description> 
  47.       </cn.itcast.demo1.City> 
  48.     </cities> 
  49.   </cn.itcast.demo1.Province> 
  50.   <cn.itcast.demo1.Province> 
  51.     <name>辽宁</name> 
  52.     <cities> 
  53.       <cn.itcast.demo1.City> 
  54.         <name>沈阳</name> 
  55.         <description>shenYang</description> 
  56.       </cn.itcast.demo1.City> 
  57.       <cn.itcast.demo1.City> 
  58.         <name>葫芦岛</name> 
  59.         <description>huLuDao</description> 
  60.       </cn.itcast.demo1.City> 
  61.     </cities> 
  62.   </cn.itcast.demo1.Province> 
  63. </list> 
  64.      */  
  65.     @Test  
  66.     public void fun1() {  
  67.         List<Province> proList = getProinvceList();  
  68.         /* 
  69.          * 创建XStream对象 
  70.          * 调用toXML把集合转换成xml字符串 
  71.          */  
  72.         XStream xstream = new XStream();  
  73.         String s = xstream.toXML(proList);  
  74.         System.out.println(s);  
  75.     }  
  76.       
  77.     /* 
  78.      * 别名(alias) 
  79.      * 希望: 
  80.      * * 默认List类型对应<list>元素,希望让List类型对应<china>元素 
  81.      * * 默认Province类型对应<cn.itcast.demo1.Province>,希望让它对应<province> 
  82.      * * 默认City类型对应<cn.itcast.demo1.City>,希望它对应<city>元素 
  83.      */  
  84.     /* 
  85. <china> 
  86.   <province> 
  87.     <name>北京</name> 
  88.     <cities> 
  89.       <city> 
  90.         <name>东城区</name> 
  91.         <description>DongChengQu</description> 
  92.       </city> 
  93.       <city> 
  94.         <name>昌平区</name> 
  95.         <description>ChangPingQu</description> 
  96.       </city> 
  97.     </cities> 
  98.   </province> 
  99.   <province> 
  100.     <name>辽宁</name> 
  101.     <cities> 
  102.       <city> 
  103.         <name>沈阳</name> 
  104.         <description>shenYang</description> 
  105.       </city> 
  106.       <city> 
  107.         <name>葫芦岛</name> 
  108.         <description>huLuDao</description> 
  109.       </city> 
  110.     </cities> 
  111.   </province> 
  112. </china> 
  113.      */  
  114.     @Test  
  115.     public void fun2() {  
  116.         List<Province> proList = getProinvceList();  
  117.         XStream xstream = new XStream();  
  118.         /* 
  119.          * 给指定的类型指定别名 
  120.          */  
  121.         xstream.alias("china", List.class);//给List类型指定别名为china  
  122.         xstream.alias("province", Province.class);//给Province指定别名为province  
  123.         xstream.alias("city", City.class);//给City类型指定别名为city  
  124.           
  125.           
  126.         String s = xstream.toXML(proList);  
  127.         System.out.println(s);  
  128.     }  
  129.       
  130.     /* 
  131.      * 默认javabean的属性都会生成子元素,而现在希望生成元素的属性 
  132.      */  
  133. /* 
  134. <china> 
  135.   <province name="北京"> 
  136.     <cities> 
  137.       <city> 
  138.         <name>东城区</name> 
  139.         <description>DongChengQu</description> 
  140.       </city> 
  141.       <city> 
  142.         <name>昌平区</name> 
  143.         <description>ChangPingQu</description> 
  144.       </city> 
  145.     </cities> 
  146.   </province> 
  147.   <province name="辽宁"> 
  148.     <cities> 
  149.       <city> 
  150.         <name>沈阳</name> 
  151.         <description>shenYang</description> 
  152.       </city> 
  153.       <city> 
  154.         <name>葫芦岛</name> 
  155.         <description>huLuDao</description> 
  156.       </city> 
  157.     </cities> 
  158.   </province> 
  159.  */  
  160.     @Test  
  161.     public void fun3() {  
  162.         List<Province> proList = getProinvceList();  
  163.         XStream xstream = new XStream();  
  164.         xstream.alias("china", List.class);//给List类型指定别名为china  
  165.         xstream.alias("province", Province.class);//给Province指定别名为province  
  166.         xstream.alias("city", City.class);//给City类型指定别名为city  
  167.           
  168.           
  169.         /* 
  170.          * 把Province类型的name属性,生成<province>元素的属性 
  171.          */  
  172.         xstream.useAttributeFor(Province.class, "name");  
  173.           
  174.           
  175.         String s = xstream.toXML(proList);  
  176.         System.out.println(s);        
  177.     }  
  178.   
  179.       
  180.     /* 
  181.      * 去除List类型的属性,只把list中的元素生成xml元素 
  182.      */  
  183. /* 
  184. <china> 
  185.   <province name="北京"> 
  186.     <city> 
  187.       <name>东城区</name> 
  188.       <description>DongChengQu</description> 
  189.     </city> 
  190.     <city> 
  191.       <name>昌平区</name> 
  192.       <description>ChangPingQu</description> 
  193.     </city> 
  194.   </province> 
  195.   <province name="辽宁"> 
  196.     <city> 
  197.       <name>沈阳</name> 
  198.       <description>shenYang</description> 
  199.     </city> 
  200.     <city> 
  201.       <name>葫芦岛</name> 
  202.       <description>huLuDao</description> 
  203.     </city> 
  204.   </province> 
  205. </china> 
  206.  */  
  207.     @Test  
  208.     public void fun4() {  
  209.         List<Province> proList = getProinvceList();  
  210.         XStream xstream = new XStream();  
  211.         xstream.alias("china", List.class);//给List类型指定别名为china  
  212.         xstream.alias("province", Province.class);//给Province指定别名为province  
  213.         xstream.alias("city", City.class);//给City类型指定别名为city  
  214.         xstream.useAttributeFor(Province.class, "name");//把Province类型的name属性,生成<province>元素的属性  
  215.           
  216.           
  217.         /* 
  218.          * 去除<cities>这样的Collection类型的属性 
  219.          * 去除Provice类的名为cities的List类型的属性! 
  220.          */  
  221.         xstream.addImplicitCollection(Province.class, "cities");  
  222.           
  223.           
  224.         String s = xstream.toXML(proList);  
  225.         System.out.println(s);        
  226.     }  
  227.       
  228.     /** 
  229.      * 去除不想要的javabean属性 
  230.      * 就是让某引起javabean属性,不生成对应的xml元素! 
  231.      */  
  232. /* 
  233. <china> 
  234.   <province name="北京"> 
  235.     <city> 
  236.       <name>东城区</name> 
  237.     </city> 
  238.     <city> 
  239.       <name>昌平区</name> 
  240.     </city> 
  241.   </province> 
  242.   <province name="辽宁"> 
  243.     <city> 
  244.       <name>沈阳</name> 
  245.     </city> 
  246.     <city> 
  247.       <name>葫芦岛</name> 
  248.     </city> 
  249.   </province> 
  250. </china> 
  251.  */  
  252.     @Test  
  253.     public void fun5() {  
  254.         List<Province> proList = getProinvceList();  
  255.         XStream xstream = new XStream();  
  256.         xstream.alias("china", List.class);//给List类型指定别名为china  
  257.         xstream.alias("province", Province.class);//给Province指定别名为province  
  258.         xstream.alias("city", City.class);//给City类型指定别名为city  
  259.         xstream.useAttributeFor(Province.class, "name");//把Province类型的name属性,生成<province>元素的属性  
  260.         xstream.addImplicitCollection(Province.class, "cities");//去除Provice类的名为cities的List类型的属性!  
  261.           
  262.           
  263.         /* 
  264.          * 让City类的,名为description属性不生成对应的xml元素 
  265.          */  
  266.         xstream.omitField(City.class, "description");  
  267.           
  268.           
  269.         String s = xstream.toXML(proList);  
  270.         System.out.println(s);        
  271.     }  
  272. }  

转载于:https://www.cnblogs.com/wlsblog/p/7367441.html

相关文章:

  • iis不能读取数据库的解决方案
  • php学习的一些笔记
  • android manifest.xml 文件
  • ireport +jasperreport 中文不能显示
  • rtmp简要流程
  • 安装office2003时提示找不到MI561407.CAB
  • 浅析MySQL中的Index Condition Pushdown (ICP 索引条件下推)和Multi-Range Read(MRR 索引多范围查找)查询优化...
  • comake2
  • MOSS 2010:Visual Studio 2010开发体验(8)——Silverlight应用
  • java 多线程 - 1
  • Teradata“统一数据架构”引领企业大数据应用体系
  • 项目经理案头手册学习系列【2】
  • 重要的转变(感觉自己最近堕落必读)
  • 2008 SQL Server优化(2)-改善SQL语句
  • ScrollView子控件高度设置无效
  • 【附node操作实例】redis简明入门系列—字符串类型
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • gcc介绍及安装
  • python_bomb----数据类型总结
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • Wamp集成环境 添加PHP的新版本
  • 高性能JavaScript阅读简记(三)
  • 汉诺塔算法
  • 聚簇索引和非聚簇索引
  • 排序算法之--选择排序
  • 如何利用MongoDB打造TOP榜小程序
  • 使用 @font-face
  • 小程序开发中的那些坑
  • 国内开源镜像站点
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #Linux(make工具和makefile文件以及makefile语法)
  • $refs 、$nextTic、动态组件、name的使用
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (solr系列:一)使用tomcat部署solr服务
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (一) storm的集群安装与配置
  • (译) 函数式 JS #1:简介
  • (转)scrum常见工具列表
  • (转)Unity3DUnity3D在android下调试
  • .NET DevOps 接入指南 | 1. GitLab 安装
  • .NET 发展历程
  • .net 中viewstate的原理和使用
  • .net/c# memcached 获取所有缓存键(keys)
  • .Net中的设计模式——Factory Method模式
  • /dev下添加设备节点的方法步骤(通过device_create)
  • @RestController注解的使用
  • @Transactional类内部访问失效原因详解
  • [ C++ ] STL---string类的模拟实现
  • []指针
  • [【JSON2WEB】 13 基于REST2SQL 和 Amis 的 SQL 查询分析器