xtream 示例介绍
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt271
1 xStream框架
xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换;
官网:
http://xstream.codehaus.org/
2 about xtream
xtream 是一个简单的工具包,用来把对象序列化成xml配置文件,并且也可以把xml反序化成对象。
4Features 功能特点
简单易用,不需要配置映射,速度快并且占用内存小,生成的xml配置文件很干净,不带额外无用信息,这样在反映序列化的时候容易读取。不需要修改序列化对象的类型。支持类图。与xmlapi 整合。详细的返回错误信息。可修改的输出 显示。
4 点型应用
传输:网络传输
持久化:生成的XML可以写到文件,做持久化。
配置:XML最常用的配置文件。
单元测试
5局限
If using the enhanced mode, XStream can re-instantiate classes that do not have a default constructor. However, if using a different JVM like an old JRockit version, a JDK 1.3 or you have restrictions because of a SecurityManager, a default constructor is required.
The enhanced mode is also necessary to restore final fields for any JDK < 1.5. This implies deserialization of instances of an inner class.
Auto-detection of annotations may cause race conditions. Preprocessing annotations is safe though.
6准备一个pojo对象
Java代码
- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
package
com.entity;
import
java.util.Date;
bpublic
class
Student {
private
int
id;
private
String name;
private
String email;
private
String address;
private
Birthday birthday;
private
Date registDate;
public
Date getRegistDate() {
return
registDate;
}
public
void
setRegistDate(Date registDate) {
this
.registDate = registDate;
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getEmail() {
return
email;
}
public
void
setEmail(String email) {
this
.email = email;
}
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address = address;
}
public
Birthday getBirthday() {
return
birthday;
}
public
void
setBirthday(Birthday birthday) {
this
.birthday = birthday;
}
// getter、setter
public
String toString() {
return
this
.name +
"#"
+
this
.id +
"#"
+
this
.address +
"#"
+
this
.birthday +
"#"
+
this
.email;
}
}
7 bean 转成 xml
测试代码:
Java代码
- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
package
com.test;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.util.Date;
import
org.junit.Before;
import
org.junit.Test;
import
com.entity.Birthday;
import
com.entity.Student;
import
com.thoughtworks.xstream.XStream;
@SuppressWarnings
(
"unchecked"
)
public
class
XStreamTest {
private
XStream xstream =
null
;
private
ObjectOutputStream out =
null
;
private
ObjectInputStream in =
null
;
private
Student bean =
null
;
@Before
public
void
init() {
try
{
xstream =
new
XStream();
bean = getTestStudent();
// xstream = new XStream(new DomDriver()); // 需要xpp3 jar
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
XStreamTest test =
new
XStreamTest();
test.init();
test.testWriteBean2XML_01();
}
public
final
void
fail(String string) {
System.out.println(string);
}
public
final
void
failRed(String string) {
System.err.println(string);
}
/**
* bean 2 XML
* */
@Test
public
void
testWriteBean2XML_01() {
try
{
fail(
"------------Bean->XML------------"
);
fail(xstream.toXML(bean));
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 类重命名后的XML
* */
@Test
public
void
testWriteBean2XML_02() {
try
{
fail(
"-----------类重命名后的XML------------"
);
// 类重命名
xstream.alias(
"student"
, Student.
class
);
xstream.aliasField(
"生日"
, Student.
class
,
"birthday"
);
xstream.aliasField(
"生日日期"
, Birthday.
class
,
"birthday"
);
fail(xstream.toXML(bean));
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 类重命名后的XML
* */
@Test
public
void
testWriteBean2XML_03() {
try
{
fail(
"-----------属性重命名后的XML------------"
);
// 属性重命名
xstream.aliasField(
"邮件"
, Student.
class
,
"email"
);
fail(xstream.toXML(bean));
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 包重命名后的XML
* */
@Test
public
void
testWriteBean2XML_04() {
try
{
fail(
"-----------包重命名后的XML------------"
);
//包重命名
xstream.aliasPackage(
"modile"
,
"com.entity"
);
fail(xstream.toXML(bean));
}
catch
(Exception e) {
e.printStackTrace();
}
}
/**
* 构造数据
* */
private
Student getTestStudent() {
Student bean =
new
Student();
bean.setAddress(
"china"
);
bean.setEmail(
"email"
);
bean.setId(
1
);
bean.setName(
"jack"
);
Birthday day =
new
Birthday();
day.setBirthday(
"2010-11-22"
);
bean.setBirthday(day);
bean.setRegistDate(
new
Date());
return
bean;
}
}
测试结果:
------------Bean->XML------------
1
2
3
4
5
6
7
8
9
10
|
< com.entity.Student > < id >1</ id > < name >jack</ name > < email >email</ email > < address >china</ address > < birthday > < birthday >2010-11-22</ birthday > </ birthday > < registDate >2011-07-11 22:33:02.359 CST</ registDate > </ com.entity.Student > |
-----------类重命名后的XML------------
1
2
3
4
5
6
7
8
9
10
|
< student > < id >1</ id > < name >jack</ name > < email >email</ email > < address >china</ address > <生日> <生日日期>2010-11-22</生日日期> </生日> < registDate >2011-07-11 22:33:02.390 CST</ registDate > </ student > |
-----------属性重命名后的XML------------
1
2
3
4
5
6
7
8
9
10
|
< com.entity.Student > < id >1</ id > < name >jack</ name > <邮件>email</邮件> < address >china</ address > < birthday > < birthday >2010-11-22</ birthday > </ birthday > < registDate >2011-07-11 22:33:02.406 CST</ registDate > </ com.entity.Student > |
-----------包重命名后的XML------------
1
2
3
4
5
6
7
8
9
10
|
< modile.Student > < id >1</ id > < name >jack</ name > < email >e</ email > < address >china</ address > < birthday > < birthday >2010-11-22</ birthday > </ birthday > < registDate >2011-07-11 22:33:02.406 CST</ registDate > </ modile.Student > |
8 List 2 XML
Java代码
- 1234567
fail(
"------------Listg<Strudent>->XML------------"
);
List<Student> list =
new
ArrayList<Student>();
list.add(bean);
Student s1 = getTestStudent();
s1.setId(
2
);
list.add(s1);
fail(xstream.toXML(list));
结果:
------------Listg<Strudent>->XML------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< list > < com.entity.Student > < id >1</ id > < name >jack</ name > < email >email</ email > < address >china</ address > < birthday > < birthday >2010-11-22</ birthday > </ birthday > < registDate >2011-07-11 22:47:08.0 CST</ registDate > </ com.entity.Student > < com.entity.Student > < id >2</ id > < name >jack</ name > < email >email</ email > < address >china</ address > < birthday > < birthday >2010-11-22</ birthday > </ birthday > < registDate >2011-07-11 22:47:08.0 CST</ registDate > </ com.entity.Student > </ list >
|
xtream 示例介绍的更多相关文章
- ASP.NET Web API 开篇示例介绍
ASP.NET Web API 开篇示例介绍 ASP.NET Web API 对于我这个初学者来说ASP.NET Web API这个框架很陌生又熟悉着. 陌生的是ASP.NET Web API是一个全 ...
- jQuery中$.fn的用法示例介绍
$.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效,下面有个不错的示例,喜欢的朋友可以参考下 如扩展$.fn.abc(),即$.fn.abc()是对jquery ...
- HTML5标签与HTML4标签的区别示例介绍_html5教程技巧
(1)概念的变化: HTML5专注内容与结构,而不专注的表现 <header> <hgroup>导航相关数据</hgroup> </header> &l ...
- CSS鼠标响应事件经过、移动、点击示例介绍
本文为大家介绍下CSS 鼠标响应事件:鼠标经过CSS.鼠标移动CSS.鼠标点击CSS以及示例,喜欢的朋友可以参考下 几种鼠标触发CSS事件. 说明: onMouseDown 按下鼠标时触发 onM ...
- Hadoop2源码分析-YARN RPC 示例介绍
1.概述 之前在<Hadoop2源码分析-RPC探索实战>一文当中介绍了Hadoop的RPC机制,今天给大家分享关于YARN的RPC的机制.下面是今天的分享目录: YARN的RPC介绍 Y ...
- 【转】javascript 中that的含义示例介绍
var that = this;,这代表什么意思呢?this代表的是当前对象,var that=this就是将当前的this对象复制一份到that变量中,下面为大家介绍这样做有什么意义 你可能会发现别 ...
- 两个示例介绍JavaScript的闭包
JavaScript的闭包有两个用途:一个是访问函数内部的变量:另一个是让变量的值在作用域内保持不变.函数是JavaScript 中唯一有作用域的对象,因此JavaScript的闭包依赖于函数实现,下 ...
- LightningChart运行互动示例介绍
LightningChart.NET完全由GPU加速,并且性能经过优化,可用于实时显示海量数据-超过10亿个数据点. LightningChart包括广泛的2D,高级3D,Polar,Smith,3D ...
- jQuery学习之prop和attr的区别示例介绍
1..prop( propertyName ) 获取匹配集合中第一个元素的Property的值 2. .prop( propertyName, value ) .prop( map ) .prop( ...
随机推荐
- node调用phantomjs-node爬取复杂页面
什么是phantomjs phantomjs官网是这么说的,'整站测试,屏幕捕获,自动翻页,网络监控',目前比较流行用来爬取复杂的,难以通过api或正则匹配的页面,比如页面是通过异步加载.phanto ...
- 亚马逊AWS EC2云实例AMI安装LNMP环境(1)——Nginx安装
概括:这里选择亚马逊EC2的Linux AMI实例,该Linux服务器是亚马逊预配置的Linux环境,内置多个YUM源,属于亚马逊首推的稳定Linux服务器.默认登录用户名为ec2-user,执行ro ...
- [补档]暑假集训D6总结
考试 不是爆零,胜似爆零= = 三道题,就拿了20分,根本没法玩好吧= = 本来以为打了道正解,打了道暴力,加上个特判分,应该不会死的太惨,然而--为啥我只有特判分啊- - 真的是惨. 讲完题觉得题是 ...
- TCP/IP 主机路由表获取
介绍在IP协议中主机的路由表获取方法: 主机初始化路由表: 直接相连路由:接口初始化时,自动获取直连主机和网络的路由信息 间接相连路由:通过执行route命令,手动初始化路由表 ICMP路由请求和通告 ...
- 再起航,我的学习笔记之JavaScript设计模式05(简单工程模式)
我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 前几 ...
- Java中容器的两种初始化方式比较
List,Set,Map的两种初始化赋值方式 List List<Integer> list2 = new ArrayList<Integer>(); for (int i= ...
- LinkQueue(链队列)
关于Node.h,请参考LinkStack #include"Node.h" template<typename ElemType> class LinkQueue { ...
- 01迷宫 洛谷 p1141
题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...
- HDU 3001 Travelling:TSP(旅行商)【节点最多经过2次】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意: 有n个城市,m条双向道路,每条道路走一次需要花费路费v.你可以将任意一个城市作为起点出发 ...
- AIX smit下创建逻辑卷、添加文件系统并挂载
--AIX smit下创建逻辑卷,添加文件系统并挂载------------------------------------------2013/10/15 首先创建逻辑卷smit lv ,这里没多大 ...