XStream详解
XStream的作用
XStream可以把JavaBean对象转换成XML!
通常服务器向客户端响应的数据都是来自数据库的一组对象,而我们不能直接把对象响应给客户端,所以我们需要把对象转换成XML再响应给客户端,这时就需要使用XStream组合了
XStream相关JAR包
我们可以到http://xstream.codehaus.org/地址去下载XStream安装包!
XStream的必导JAR包:
核心JAR包:xstream-1.4.7.jar;
必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器)
XStream举例
首先创建两个JavaBean对象
City类----------------------------
public class City { private String name;
private String description; public City(String name,String description) {
this.name=name;
this.description=description;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "City [name=" + name + ", description=" + description + "]";
} }
Province类--------------------------
public class Province { private String name;
private List<City> cities = new ArrayList<City>(); public Province(String name) {
this.name=name;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<City> getCities() {
return cities;
} public void addCity(City name) {
cities.add(name);
}
@Override
public String toString() {
return "Province [name=" + name + ", cities=" + cities + "]";
} }
接下来,我们需要写一个main(),创建一个List,List中存放两个Province对象!最终我们把List转换成XML
下面是使用XStream转换list为XML的代码
Province p1 = new Province("辽宁省");
p1.addCity(new City("沈阳", "shenyang"));
p1.addCity(new City("大连", "dalian"));
Province p2 = new Province("吉林省");
p2.addCity(new City("长春", "changchen"));
p2.addCity(new City("白城", "baicheng"));
List<Province> list = new ArrayList<Province>();
list.add(p1);
list.add(p2);
//--------------------------------------------------------
XStream xstream = new XStream();
String s = xstream.toXML(list);
System.out.println(s);
下面是运行结果
<list>
<cn.itcast.xstream.demo1.Province>
<name>辽宁省</name>
<cities>
<cn.itcast.xstream.demo1.City>
<name>沈阳</name>
<description>shenyang</description>
</cn.itcast.xstream.demo1.City>
<cn.itcast.xstream.demo1.City>
<name>大连</name>
<description>dalian</description>
</cn.itcast.xstream.demo1.City>
</cities>
</cn.itcast.xstream.demo1.Province>
<cn.itcast.xstream.demo1.Province>
<name>吉林省</name>
<cities>
<cn.itcast.xstream.demo1.City>
<name>长春</name>
<description>changchen</description>
</cn.itcast.xstream.demo1.City>
<cn.itcast.xstream.demo1.City>
<name>白城</name>
<description>baicheng</description>
</cn.itcast.xstream.demo1.City>
</cities>
</cn.itcast.xstream.demo1.Province>
</list>
使用alias,在生成的XML中,与类名对应的元素名称包含了包名部分,这很不好看!若想自定义生成的元素名称,需要使用XStream为类名提供别名:
xstream.alias("province", Province.class);
xstream.alias("china", List.class);
xstream.alias("city", City.class);
---------------------------------------------------------------------------------------
结果为:
<china>
<province>
<name>辽宁省</name>
<cities>
<city>
<name>沈阳</name>
<description>shenyang</description>
</city>
<city>
<name>大连</name>
<description>dalian</description>
</city>
</cities>
</province>
<province>
<name>吉林省</name>
<cities>
<city>
<name>长春</name>
<description>changchen</description>
</city>
<city>
<name>白城</name>
<description>baicheng</description>
</city>
</cities>
</province>
</china>
把子元素变为元素属性,例如我们需要把<province>的子元素<name>变成:<province name=””>样式,那么需要调用如下方法
xstream.useAttributeFor(Province.class, "name");
--------------------------------------------------------------
结果为
<china>
<province name="辽宁省">
<cities>
<city>
<name>沈阳</name>
<description>shenyang</description>
</city>
<city>
<name>大连</name>
<description>dalian</description>
</city>
</cities>
</province>
<province name="吉林省">
<cities>
<city>
<name>长春</name>
<description>changchen</description>
</city>
<city>
<name>白城</name>
<description>baicheng</description>
</city>
</cities>
</province>
</china>
去除集合属性对应元素
因为Pronvice类有一个cities成员,所以生成了<cities>元素,但这个元素对XML文档而言没有什么意义,所以我们希望把它去除!
xstream.addImplicitCollection(Province.class, "cities");
--------------------------------------------------------------------------
结果为
<china>
<province name="辽宁省">
<city>
<name>沈阳</name>
<description>shenyang</description>
</city>
<city>
<name>大连</name>
<description>dalian</description>
</city>
</province>
<province name="吉林省">
<city>
<name>长春</name>
<description>changchen</description>
</city>
<city>
<name>白城</name>
<description>baicheng</description>
</city>
</province>
</china>
让类的成员不生成对应XML元素
每个类,每个成员都有对应的元素(或属性)存在,但有时我们并不希望某些类的成员在对应的XML文档中出现,例如我们不希望City类的description成员出现在XML文档中,可以使用下面方法
xstream.omitField(City.class, "description"); //不希望description属性出现
----------------------------------------------------------
结果为
<china>
<province name="辽宁省">
<city>
<name>沈阳</name>
</city>
<city>
<name>大连</name>
</city>
</province>
<province name="吉林省">
<city>
<name>长春</name>
</city>
<city>
<name>白城</name>
</city>
</province>
</china>
XStream详解的更多相关文章
- Spring MVC测试框架详解——服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- SpringMvc测试框架详解----服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- Spring学习(一)-----Spring 模块详解
官方下载链接:http://repo.spring.io/release/org/springframework/spring/ Spring 模块详解: Core 模块 spring-beans-3 ...
- struts-2.3.24.1中的jar的详解
Struts2.3.24.1核心Jar包详解 antlr-2.7.2.jar 语言转换工具,它是接受词文法语言描述,并能产生识别这些语言的语句的程序的一种工具 a ...
- maven打包插件详解
maven-jar-plugin插件的使用及详解 该插件的xml配置及详解如下: <plugin> <groupId>org.apache.maven.plugins</ ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
随机推荐
- 不基于比较的排序算法:Counting-sort和Radix-sort
- dubbo学习笔记三(全注解)
完全用注解替换掉之前的部分配置文件 项目结构 下面给出服务的的部分代码 [DubboConfiguration] @Configuration @EnableDubbo(scanBasePackage ...
- 16、Nginx Rewrite重写
1.Rewrite基本概述 1.1.什么是rewrite Rewrite主要实现url地址重写, 以及地址重定向,就是将用户请求web服务器的地址重新定向到其他URL的过程. 1.2.Rewrite使 ...
- Ubuntu中用sudo apt-get install makeinfo时,出错:Unable to locate package
背景: 在准备ARM交叉编译环境时,执行命令: DISTRO=fsl-imx-x11 MACHINE=imx6qsabresd source fsl-setup-release.sh -b build ...
- verilog分频模块设计
verilog设计: 分频器的设计: 分频器就是将一个时钟源的频率降低的过程(可以通过观察分频之后周期中包含几个原时钟周期来看是几分频),分频分为基数分频也分为偶数分频, 偶数分频的代码如下:(其中就 ...
- Linux进程管理工具之ps
1.PS进程管理指令 ps -aux USER:用户名称 PID:进程号 %CPU:进程占用CPU的百分比 %MEM:进程占用物理内存的百分比 VSZ:进程占用的虚拟内存大小(单位:KB) RS ...
- Ext4文件系统修复
Ext4文件系统修复 目录 一. super block........................................................................ ...
- C# JSON的序列化与反序列化
需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然后使用Using: using System.Runtime.Serial ...
- 简述Hibernate常见优化策略
①制定合理的缓存策略 ② 采用合理的Session管理机制 ③ 尽量使用延迟加载特性 ④如果可以, 选用基于version的乐观锁替代悲观锁 ⑤在开发过程中, 开启hibernate.show_sql ...
- qt5--列表控件QListWidget
需要 #include <QListWidget> #include <QListWidgetItem> 列表控件可以让我们以列表形式呈现内容,是界面 ...