XStream中几个注解的含义和用法
转自:http://blog.csdn.net/robert_mm/article/details/8459879
XStream是个很强大的工具,能将java对象和xml之间相互转化。xstream不在意java类中成员变量是私有还是公有,也不在乎是否有默认构造函数。它调用方式也非常简单:从xml对象转化为java对象,使用fromXML()方法;从java对象序列化为xml,toXML()即可,很方便。xstream也支持注解方式,这些都是为了简化输出而设计,下面为大家简单说一下这几个注解的含义和用法。
1. 当没有任何注解情况下
- public class Cat {
- //名字
- private String name;
- //大小
- private Integer age;
- //玩具球 球具有颜色属性
- private List<Ball> balls;
- Cat(String name,Integer age,List<Ball> balls){
- this.name = name;
- this.age = age;
- this.balls=balls;
- }
- //getter/setter方法 为了赋值使用
其中Ball定义如下:
- public class Ball {
- //颜色
- private String color;
- Ball(String color){
- this.color = color;
- }
没有任何注解,输出如下:
- public static void main(String[] args) throws Exception{
- //初始化cat
- List<Ball> balls = new ArrayList<Ball>();
- balls.add(new Ball("red"));
- Cat cat = new Cat("馄饨",1,balls);
- //初始化结束
- //为了方便查找 将文件制定到D盘cat.xml中
- FileOutputStream fout = new FileOutputStream("D:/cat.xml");
- XStream xs = new XStream();
- xs.toXML(cat,fout);
- }<span style="font-size:14px;">
- </span>
得到Cat.xml如下
- <com.timejob.node.Cat>
- <name>馄饨</name>
- <age>1</age>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </com.timejob.node.Cat>
1. @XStreamAlias("cat") 等同于 xstream.alias("cat", Cat.class);
- @XStreamAlias("cat") //here
- public class Cat {
- //名字
- private String name;
- //大小
- private Integer age;
- //玩具球 球具有颜色属性
- private List<Ball> balls;
我们需要明确给出,哪个类的注解需要被激活:
- public static void main(String[] args) throws Exception{
- //初始化cat
- List<Ball> balls = new ArrayList<Ball>();
- balls.add(new Ball("red"));
- Cat cat = new Cat("馄饨",1,balls);
- //初始化结束
- //为了方便查找 将文件制定到D盘cat.xml中
- FileOutputStream fout = new FileOutputStream("D:/cat.xml");
- XStream xs = new XStream();
- //xs.alias("cat", Cat.class); //等同于 @XStreamAlias("cat")
- xs.processAnnotations(Cat.class);//将Cat.class类上的注解将会使用
- xs.toXML(cat,fout);
- }
当我们在Cat类名使用该注解时,表明该类序列化为xml时,类名com.timejob.node.Cat将替换成cat输出,这样使得xml更清晰简短:
- <cat>
- <name>馄饨</name>
- <age>1</age>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </cat>
2. XStreamAsAttribute 作用是将类内成员作为父节点属性输出,等同于xstream.useAttributeFor(Cat.class, "name")
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute // here 将name作为Cat属性输出在父节点
- private String name;
- //大小
- private Integer age;
- //玩具球 球具有颜色属性
- private List<Ball> balls;
其他代码保持不变,输出后cat.xml如下:
- <cat name="馄饨">
- <age>1</age>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </cat>
我们看到name属性已经作为 cat的属性输出在根节点上
3. @XStreamAlias 作用就是将属性按照别名输出,等同于xstream.aliasField("catAge", Cat.class, "age");
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute
- private String name;
- //大小
- @XStreamAlias("catAge") //here
- private Integer age;
- //玩具球 球具有颜色属性
- private List<Ball> balls;
得到cat.xml文件如下:
- <cat name="馄饨">
- <catAge>1</catAge>
- <balls>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </balls>
- </cat>
4.对于集合常用的注解 @XStreamImplicit 去除<Balls></Balls>显示,只显示之间的<Ball></Ball>节点元素
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute
- private String name;
- //大小
- @XStreamAlias("catAge")
- private Integer age;
- //玩具球 球具有颜色属性
- @XStreamImplicit //here
- private List<Ball> balls;
此时输出如下:
- <cat name="馄饨">
- <catAge>1</catAge>
- <com.timejob.node.Ball>
- <color>red</color>
- </com.timejob.node.Ball>
- </cat>
这样看起来就更加简洁了。
4.还有几个注解,都是比较好理解的,如@XStreamOmitField 表明该属性不会被序列化到xml中。
- @XStreamAlias("cat")
- public class Cat {
- //名字
- @XStreamAsAttribute
- private String name;
- //大小
- @XStreamAlias("catAge")
- private Integer age;
- //玩具球 球具有颜色属性
- @XStreamImplicit
- private List<Ball> balls;
- @XStreamOmitField //here
- private String noCare;
常用的就这么几个,其他的再讨论和学习吧。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
以下为原创:
@XStreamImplicit(itemFieldName="ballName")
itemFieldName支持修改list中每个元素的节点的名字,如下:
、、、、、、、、、、、、、、、
import com.thoughtworks.xstream.XStream; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List; public class Test {
public static void main(String[] args) {
//初始化cat
List<Ball> balls = new ArrayList<Ball>();
balls.add(new Ball("red"));
balls.add(new Ball("blue"));
Cat cat = new Cat("馄饨",1,balls);
//初始化结束
//为了方便查找 将文件制定到D盘cat.xml中
FileOutputStream fout = null;
try {
fout = new FileOutputStream("D:/cat.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XStream xs = new XStream();
xs.processAnnotations(Cat.class);//将Cat.class类上的注解将会使用
xs.toXML(cat,fout);
}
} 将输出:
<com.jianwu.boss.domain.Cat>
<name>馄饨</name>
<age>1</age>
<ballName>
<color>red</color>
</ballName>
<ballName>
<color>blue</color>
</ballName>
</com.jianwu.boss.domain.Cat>
注意,
xs.processAnnotations(Cat.class);//将Cat.class类上的注解将会使用
这句一定要加上,这句才能使注解生效。
XStream中几个注解的含义和用法的更多相关文章
- xstream中几个注解的含义和用法(转)
XStream是个很强大的工具,能将Java对象和xml之间相互转化.xstream不在意java类中成员变量是私有还是公有,也不在乎是否有默认构造函数.它调用方式也非常简单:从xml对象转化为jav ...
- UML类图中箭头和线条的含义和用法
UML类图中箭头和线条的含义和用法 在学习UML过程中,你经常会遇到UML类图关系,这里就向大家介绍一下UML箭头.线条代表的意义,相信通过本文的介绍你对UML中箭头.线条的意义有更明确的认识. AD ...
- SSM框架中常用的注解及含义
@Controller---使用它标记在一个类上,dispatcher会扫描使用该注解类的方法,并检测该方法是否使用了@RequestMapping注解,加上RequestMapping注解的方法才是 ...
- 2.aop中几个注解的含义
参考地址:http://elim.iteye.com/blog/2395255
- 【转载】UML类图中箭头和线条的含义和用法
文章转载自 http://blog.csdn.net/hewei0241/article/details/7674450 https://blog.csdn.net/iamherego/article ...
- word 中Sentences、Paragraph等含义和用法
word 中有Words,Characters,Sentences.Paragraph,Sections 具体含义如下表达式 含义 返回的对象 Words(index) ...
- ajax中的一些参数的含义及用法
jquery中的ajax方法参数总结: 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get) ...
- (转)C++ main函数中参数argc和argv含义及用法
原博地址:https://blog.csdn.net/dcrmg/article/details/51987413 argc 是 argument count的缩写,表示传入main函数的参数个数: ...
- C++ main函数中参数argc和argv含义及用法
argc 是 argument count的缩写,表示传入main函数的参数个数: argv 是 argument vector的缩写,表示传入main函数的参数序列或指针,并且第一个参数argv[0 ...
随机推荐
- 什么是Coded UI
什么是Coded UI Coded UI Test是Visual Studio 2010对于Testing Project(测试工程)提供的关于UI自动化测试的框架,支持Win32,Web,WPF等U ...
- unity, AnimatorCullingMode的一个bug
我在一个fbx节点上添加了一个Animator,CullingMode设置为Cull Update Transforms(即如果没有激活的SkinnedRenderer就不更新骨骼动画),然后我将这个 ...
- CentOS 5.4 安装和卸载桌面
显示系统已经安装的组件,和可以安装的组件:#yum grouplist 如果系统安装之初采用最小化安装,没有安装xwindow,那么先安装:#yum groupinstall "X Wind ...
- php检测iis环境是否支持htaccess
php检测iis环境是否支持htaccess的方法. modrewrite.php <?php echo "mod_rewrite works"; ?> open_me ...
- [svc]salt-jinja模版
实现不同机器的差异化配置 把apache监听的端口统一改为8080 把配置文件files/httpd.conf 文件做成模版 修改lamp.sls改模版变量赋值 执行看结果: ok come on. ...
- 【Android】15.0 第15章 广播和通知—本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-02-28 一.简介 广播(Broadcast):其功能类似于收音机的广播,你只要调到那个台(只要在接收的类中注册了要接收的广播),就能 ...
- 进程控制函数(2)-setpgid() 修改当前进程的进程组ID
定义:int setpgid(pid_t pid,pid_t pgid); 表头文件:#include<unistd.h> 说明:setpgid()将参数pid 指定进程所属的组识别码设为 ...
- DelphiXE8FMX工程实现无边框托动(发送消息)
1.引用单元 uses Winapi.Windows, FMX.Platform.Win, Winapi.Messages; 2.发送消息 //发送系统消息SendMessage(FmxHandleT ...
- Eclipse4.4 安装java反编译插件Eclipse Class Decompiler
一.在线安装方式: Eclipse Class Decompiler整合了眼下最好的2个Java反编译工具Jad和JD-Core,而且和Eclipse Class Viewer无缝集成.可以非常方便的 ...
- SenCha Touch 与 EXTJS 安装Myeclipse 插件
http://www.cnblogs.com/jirimutu01/default.html 关于SenchaEclipsePlugin插件的安装和使用 使用过eclipse开发java程序的人都知道 ...