XStream(xml/bean转换)
XStream
1. 什么作用
* 可以把JavaBean转换为(序列化为)xml
* 核心JAR包:xstream-1.4.7.jar;
* 必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);
* XStream xstream = new XStream();
* String xmlStr = xstream.toXML(javabean);
* 别名:把类型对应的元素名修改了
> 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的对应的元素!
具体实例
- package cn.itcast.demo1;
- import java.util.ArrayList;
- import java.util.List;
- import org.junit.Test;
- import com.thoughtworks.xstream.XStream;
- /**
- * 演示XStream
- * @author cxf
- *
- */
- public class Demo1 {
- // 返回javabean集合
- public List<Province> getProinvceList() {
- Province p1 = new Province();
- p1.setName("北京");
- p1.addCity(new City("东城区", "DongChengQu"));
- p1.addCity(new City("昌平区", "ChangPingQu"));
- Province p2 = new Province();
- p2.setName("辽宁");
- p2.addCity(new City("沈阳", "shenYang"));
- p2.addCity(new City("葫芦岛", "huLuDao"));
- List<Province> provinceList = new ArrayList<Province>();
- provinceList.add(p1);
- provinceList.add(p2);
- return provinceList;
- }
- /**
- <list> --> List类型显示list
- <cn.itcast.demo1.Province> --> javabean的类型为Province,它元素的名称为类的完整名
- <name>北京</name> --> javabean的属性名
- <cities> --> javabean的属性名
- <cn.itcast.demo1.City> --> 类名
- <name>东城区</name> --> 属性名
- <description>DongChengQu</description> --> 属性名
- </cn.itcast.demo1.City>
- <cn.itcast.demo1.City>
- <name>昌平区</name>
- <description>ChangPingQu</description>
- </cn.itcast.demo1.City>
- </cities>
- </cn.itcast.demo1.Province>
- <cn.itcast.demo1.Province>
- <name>辽宁</name>
- <cities>
- <cn.itcast.demo1.City>
- <name>沈阳</name>
- <description>shenYang</description>
- </cn.itcast.demo1.City>
- <cn.itcast.demo1.City>
- <name>葫芦岛</name>
- <description>huLuDao</description>
- </cn.itcast.demo1.City>
- </cities>
- </cn.itcast.demo1.Province>
- </list>
- */
- @Test
- public void fun1() {
- List<Province> proList = getProinvceList();
- /*
- * 创建XStream对象
- * 调用toXML把集合转换成xml字符串
- */
- XStream xstream = new XStream();
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- /*
- * 别名(alias)
- * 希望:
- * * 默认List类型对应<list>元素,希望让List类型对应<china>元素
- * * 默认Province类型对应<cn.itcast.demo1.Province>,希望让它对应<province>
- * * 默认City类型对应<cn.itcast.demo1.City>,希望它对应<city>元素
- */
- /*
- <china>
- <province>
- <name>北京</name>
- <cities>
- <city>
- <name>东城区</name>
- <description>DongChengQu</description>
- </city>
- <city>
- <name>昌平区</name>
- <description>ChangPingQu</description>
- </city>
- </cities>
- </province>
- <province>
- <name>辽宁</name>
- <cities>
- <city>
- <name>沈阳</name>
- <description>shenYang</description>
- </city>
- <city>
- <name>葫芦岛</name>
- <description>huLuDao</description>
- </city>
- </cities>
- </province>
- </china>
- */
- @Test
- public void fun2() {
- List<Province> proList = getProinvceList();
- XStream xstream = new XStream();
- /*
- * 给指定的类型指定别名
- */
- xstream.alias("china", List.class);//给List类型指定别名为china
- xstream.alias("province", Province.class);//给Province指定别名为province
- xstream.alias("city", City.class);//给City类型指定别名为city
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- /*
- * 默认javabean的属性都会生成子元素,而现在希望生成元素的属性
- */
- /*
- <china>
- <province name="北京">
- <cities>
- <city>
- <name>东城区</name>
- <description>DongChengQu</description>
- </city>
- <city>
- <name>昌平区</name>
- <description>ChangPingQu</description>
- </city>
- </cities>
- </province>
- <province name="辽宁">
- <cities>
- <city>
- <name>沈阳</name>
- <description>shenYang</description>
- </city>
- <city>
- <name>葫芦岛</name>
- <description>huLuDao</description>
- </city>
- </cities>
- </province>
- */
- @Test
- public void fun3() {
- List<Province> proList = getProinvceList();
- XStream xstream = new XStream();
- xstream.alias("china", List.class);//给List类型指定别名为china
- xstream.alias("province", Province.class);//给Province指定别名为province
- xstream.alias("city", City.class);//给City类型指定别名为city
- /*
- * 把Province类型的name属性,生成<province>元素的属性
- */
- xstream.useAttributeFor(Province.class, "name");
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- /*
- * 去除List类型的属性,只把list中的元素生成xml元素
- */
- /*
- <china>
- <province name="北京">
- <city>
- <name>东城区</name>
- <description>DongChengQu</description>
- </city>
- <city>
- <name>昌平区</name>
- <description>ChangPingQu</description>
- </city>
- </province>
- <province name="辽宁">
- <city>
- <name>沈阳</name>
- <description>shenYang</description>
- </city>
- <city>
- <name>葫芦岛</name>
- <description>huLuDao</description>
- </city>
- </province>
- </china>
- */
- @Test
- public void fun4() {
- List<Province> proList = getProinvceList();
- XStream xstream = new XStream();
- xstream.alias("china", List.class);//给List类型指定别名为china
- xstream.alias("province", Province.class);//给Province指定别名为province
- xstream.alias("city", City.class);//给City类型指定别名为city
- xstream.useAttributeFor(Province.class, "name");//把Province类型的name属性,生成<province>元素的属性
- /*
- * 去除<cities>这样的Collection类型的属性
- * 去除Provice类的名为cities的List类型的属性!
- */
- xstream.addImplicitCollection(Province.class, "cities");
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- /**
- * 去除不想要的javabean属性
- * 就是让某引起javabean属性,不生成对应的xml元素!
- */
- /*
- <china>
- <province name="北京">
- <city>
- <name>东城区</name>
- </city>
- <city>
- <name>昌平区</name>
- </city>
- </province>
- <province name="辽宁">
- <city>
- <name>沈阳</name>
- </city>
- <city>
- <name>葫芦岛</name>
- </city>
- </province>
- </china>
- */
- @Test
- public void fun5() {
- List<Province> proList = getProinvceList();
- XStream xstream = new XStream();
- xstream.alias("china", List.class);//给List类型指定别名为china
- xstream.alias("province", Province.class);//给Province指定别名为province
- xstream.alias("city", City.class);//给City类型指定别名为city
- xstream.useAttributeFor(Province.class, "name");//把Province类型的name属性,生成<province>元素的属性
- xstream.addImplicitCollection(Province.class, "cities");//去除Provice类的名为cities的List类型的属性!
- /*
- * 让City类的,名为description属性不生成对应的xml元素
- */
- xstream.omitField(City.class, "description");
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- }
XStream(xml/bean转换)的更多相关文章
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析 --转
SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 原文地址:http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-c ...
- SpringMVC源码阅读:Json,Xml自动转换
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...
- Spring源码-IOC部分-Xml Bean解析注册过程【3】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]
记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...
- 将bean转换成键值列表
日常开发中在进行接口对接的数据传输时,有一种场景是将bean转成jsonString,这里可以将bean转换成Map再转成jsonString. 工具类如下: public static String ...
- 利用JAXB实现java实体类和xml互相转换
1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同 ...
- 将Xml字符串转换成(DataTable || DataSet || XML)对象
今天用到一个功能:就是把从数据库读出来的内容转换成XML字符串流格式,并输出给一个功能函数.在写的过程,为方便以后的使用,我对这一功能进行分装.该类的具体格式如下:XmlConvert类命名空间:Ni ...
- json串转化成xml文件、xml文件转换成json串
1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...
- JAXB实现java对象与xml之间转换
JAXB简介: 1.JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标 ...
随机推荐
- linux tcp server demo
#include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <ne ...
- one2many &&many2many
只记录双向的情况(双向是单向的一种) @OneToMany 和 @ManyToOne :一个Group 包含多个 User; Group.class package com.XX.model; im ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- oracle 索引使用小结
1. 普通索引 create index my_index on test (col_1); 可创建合并两列或多列的索引,最多可将32列合并在一个索引中(位图索引最多可合并30列) create in ...
- 转:Ubuntu下下载工具安装--uget+aria2
原文地址:http://burner1024.blog.163.com/blog/static/17447800420126191858424/ Windows下的下载工具--迅雷,之所以下载速度快, ...
- Linux 下 实现 锐捷验证的方式
准备工作:下载mentohust并安装 步骤: 1.打开终端,输入sudo mentohust 2.配置相关参数,网卡选第一个,用户名密码自己输入,类型选锐捷私有,DHCP选认证前.完成后回车即可通过 ...
- cento7.3下玩转sphinx
cento7.5下玩转sphinx 1 安装依赖文件 yum install postgresql-libs unixODBC 2 下载 wget http://sphinxsearch.com/fi ...
- 关于OpenGL Framebuffer Object、glReadPixels与离屏渲染
最近写论文需要用到离屏渲染(主要是因为模型太大普通窗口绘制根本做不了),于是翻阅了红宝书查了下相关api和用法.中文版的红宝书可读性有点差,很多地方翻译地晦涩,但好歹读起来比较快,主要相关章节为第8章 ...
- Resetting the Root Password Using rd.break for RHEL7
Start the system and, on the GRUB 2 boot screen, press the e key for edit. Remove the rhgb and quiet ...
- 远程Servie通信AIDL
不可以直接通过binder了. 1.先编写一个aidl文件,里边包含我们要通信的方法.(Android studio 有直接新建AIDL选项) interface myInterface { /** ...