XStream是个很强大的工具,能将Java对象和xml之间相互转化。xstream不在意java类中成员变量是私有还是公有,也不在乎是否有默认构造函数。它调用方式也非常简单:从xml对象转化为java对象,使用fromXML()方法;从java对象序列化为xml,toXML()即可,很方便。xstream也支持注解方式,这些都是为了简化输出而设计,下面为大家简单说一下这几个注解的含义和用法。

1. 当没有任何注解情况下

  1. public class Cat {
  2. //名字
  3. private String name;
  4. //大小
  5. private Integer age;
  6. //玩具球  球具有颜色属性
  7. private List<Ball> balls;
  8. Cat(String name,Integer age,List<Ball> balls){
  9. this.name = name;
  10. this.age = age;
  11. this.balls=balls;
  12. }
  13. //getter/setter方法 为了赋值使用

其中Ball定义如下:

  1. public class Ball {
  2. //颜色
  3. private String color;
  4. Ball(String color){
  5. this.color = color;
  6. }

没有任何注解,输出如下:

  1. public static void main(String[] args) throws Exception{
  2. //初始化cat
  3. List<Ball> balls = new ArrayList<Ball>();
  4. balls.add(new Ball("red"));
  5. Cat cat = new Cat("馄饨",1,balls);
  6. //初始化结束
  7. //为了方便查找 将文件制定到D盘cat.xml中
  8. FileOutputStream fout = new FileOutputStream("D:/cat.xml");
  9. XStream xs = new XStream();
  10. xs.toXML(cat,fout);
  11. }<span style="font-size:14px;">
  12. </span>

得到Cat.xml如下

  1. <com.timejob.node.Cat>
  2. <name>馄饨</name>
  3. <age>1</age>
  4. <balls>
  5. <com.timejob.node.Ball>
  6. <color>red</color>
  7. </com.timejob.node.Ball>
  8. </balls>
  9. </com.timejob.node.Cat>

1. @XStreamAlias("cat") 等同于 xstream.alias("cat", Cat.class);

  1. @XStreamAlias("cat")  //here
  2. public class Cat {
  3. //名字
  4. private String name;
  5. //大小
  6. private Integer age;
  7. //玩具球  球具有颜色属性
  8. private List<Ball> balls;

我们需要明确给出,哪个类的注解需要被激活:

  1. public static void main(String[] args) throws Exception{
  2. //初始化cat
  3. List<Ball> balls = new ArrayList<Ball>();
  4. balls.add(new Ball("red"));
  5. Cat cat = new Cat("馄饨",1,balls);
  6. //初始化结束
  7. //为了方便查找 将文件制定到D盘cat.xml中
  8. FileOutputStream fout = new FileOutputStream("D:/cat.xml");
  9. XStream xs = new XStream();
  10. //xs.alias("cat", Cat.class); //等同于 @XStreamAlias("cat")
  11. xs.processAnnotations(Cat.class);//将Cat.class类上的注解将会使用
  12. xs.toXML(cat,fout);
  13. }

当我们在Cat类名使用该注解时,表明该类序列化为xml时,类名com.timejob.node.Cat将替换成cat输出,这样使得xml更清晰简短:

  1. <cat>
  2. <name>馄饨</name>
  3. <age>1</age>
  4. <balls>
  5. <com.timejob.node.Ball>
  6. <color>red</color>
  7. </com.timejob.node.Ball>
  8. </balls>
  9. </cat>

2. XStreamAsAttribute 作用是将类内成员作为父节点属性输出,等同于xstream.useAttributeFor(Cat.class, "name")

  1. @XStreamAlias("cat")
  2. public class Cat {
  3. //名字
  4. @XStreamAsAttribute // here  将name作为Cat属性输出在父节点
  5. private String name;
  6. //大小
  7. private Integer age;
  8. //玩具球  球具有颜色属性
  9. private List<Ball> balls;

其他代码保持不变,输出后cat.xml如下:

  1. <cat name="馄饨">
  2. <age>1</age>
  3. <balls>
  4. <com.timejob.node.Ball>
  5. <color>red</color>
  6. </com.timejob.node.Ball>
  7. </balls>
  8. </cat>

我们看到name属性已经作为 cat的属性输出在根节点上

3. @XStreamAlias 作用就是将属性按照别名输出,等同于xstream.aliasField("catAge", Cat.class, "age");

  1. @XStreamAlias("cat")
  2. public class Cat {
  3. //名字
  4. @XStreamAsAttribute
  5. private String name;
  6. //大小
  7. @XStreamAlias("catAge")  //here
  8. private Integer age;
  9. //玩具球  球具有颜色属性
  10. private List<Ball> balls;

得到cat.xml文件如下:

  1. <cat name="馄饨">
  2. <catAge>1</catAge>
  3. <balls>
  4. <com.timejob.node.Ball>
  5. <color>red</color>
  6. </com.timejob.node.Ball>
  7. </balls>
  8. </cat>

4.对于集合常用的注解 @XStreamImplicit 去除<Balls></Balls>显示,只显示之间的<Ball></Ball>节点元素

  1. @XStreamAlias("cat")
  2. public class Cat {
  3. //名字
  4. @XStreamAsAttribute
  5. private String name;
  6. //大小
  7. @XStreamAlias("catAge")
  8. private Integer age;
  9. //玩具球  球具有颜色属性
  10. @XStreamImplicit    //here
  11. private List<Ball> balls;

此时输出如下:

  1. <cat name="馄饨">
  2. <catAge>1</catAge>
  3. <com.timejob.node.Ball>
  4. <color>red</color>
  5. </com.timejob.node.Ball>
  6. </cat>

这样看起来就更加简洁了。

4.还有几个注解,都是比较好理解的,如@XStreamOmitField 表明该属性不会被序列化到xml中。

  1. @XStreamAlias("cat")
  2. public class Cat {
  3. //名字
  4. @XStreamAsAttribute
  5. private String name;
  6. //大小
  7. @XStreamAlias("catAge")
  8. private Integer age;
  9. //玩具球  球具有颜色属性
  10. @XStreamImplicit
  11. private List<Ball> balls;
  12. @XStreamOmitField        //here
  13. private String noCare;

常用的就这么几个,其他的再讨论和学习吧。

xstream中几个注解的含义和用法(转)的更多相关文章

  1. XStream中几个注解的含义和用法

    转自:http://blog.csdn.net/robert_mm/article/details/8459879 XStream是个很强大的工具,能将java对象和xml之间相互转化.xstream ...

  2. UML类图中箭头和线条的含义和用法

    UML类图中箭头和线条的含义和用法 在学习UML过程中,你经常会遇到UML类图关系,这里就向大家介绍一下UML箭头.线条代表的意义,相信通过本文的介绍你对UML中箭头.线条的意义有更明确的认识. AD ...

  3. SSM框架中常用的注解及含义

    @Controller---使用它标记在一个类上,dispatcher会扫描使用该注解类的方法,并检测该方法是否使用了@RequestMapping注解,加上RequestMapping注解的方法才是 ...

  4. 2.aop中几个注解的含义

    参考地址:http://elim.iteye.com/blog/2395255

  5. 【转载】UML类图中箭头和线条的含义和用法

    文章转载自 http://blog.csdn.net/hewei0241/article/details/7674450 https://blog.csdn.net/iamherego/article ...

  6. word 中Sentences、Paragraph等含义和用法

    word 中有Words,Characters,Sentences.Paragraph,Sections 具体含义如下表达式             含义   返回的对象 Words(index)  ...

  7. ajax中的一些参数的含义及用法

    jquery中的ajax方法参数总结: 1.url:  要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type:  要求为String类型的参数,请求方式(post或get) ...

  8. (转)C++ main函数中参数argc和argv含义及用法

    原博地址:https://blog.csdn.net/dcrmg/article/details/51987413 argc 是 argument count的缩写,表示传入main函数的参数个数: ...

  9. C++ main函数中参数argc和argv含义及用法

    argc 是 argument count的缩写,表示传入main函数的参数个数: argv 是 argument vector的缩写,表示传入main函数的参数序列或指针,并且第一个参数argv[0 ...

随机推荐

  1. 报错分析---->jsp自定义标签:Unable to load tag handler class

    Unable to load tag handler class 无法加载标签处理程序类 处理自定义标签的类中如下: 调用自定义标签的jsp中如下:

  2. Linux压缩解压缩(unzip,tar)

    unzip tar 常用解压缩命令: tar -zxvpf:解压缩 tar -zcvpf: 压缩 # tar [-j|-z] [cv] [-f 建立的檔名] filename... <==打包与 ...

  3. http请求及json发送与解析 post string

    golang http请求及json流解析 - 长风v持成的博客 - CSDN博客 https://blog.csdn.net/u011677067/article/details/80852158 ...

  4. 新团建立时间 timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

    w 不根据当前时间戳更新. `wtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,

  5. Spark源码分析 – SparkContext

    Spark源码分析之-scheduler模块 这位写的非常好, 让我对Spark的源码分析, 变的轻松了许多 这里自己再梳理一遍 先看一个简单的spark操作, val sc = new SparkC ...

  6. Linux 搭建Git服务器

    安装Git yum install -y git git --version 创建 Git 用户 sudo adduser git // 设置密码 passwd git 导入公钥 find / -na ...

  7. window下cmd的宽度调整

    一直被cmd的宽度这么了好些年,刚才搜索了下,还真可以设置.标题栏右键属性,调整屏幕缓冲区宽度,只要足够长就不会换行了,然后调整窗口大小-宽度,不能超过屏幕缓冲区宽度,当小于屏幕缓冲区的时候,就会显示 ...

  8. 使用Standford coreNLP进行中文命名实体识别

    因为工作需要,调研了一下Stanford coreNLP的命名实体识别功能. Stanford CoreNLP是一个比较厉害的自然语言处理工具,很多模型都是基于深度学习方法训练得到的. 先附上其官网链 ...

  9. golang处理 json中非法字符

    原文: Hi there, I just discovered Go and decided to port a little program to Go. The program reads JSO ...

  10. MFC工具栏设计

    工具栏中包含了一组用于执行命令的按钮,每个按钮都用一个图标来表示.当单击某个按钮时,会产生一个相应的消息,对这个消息的处理就是按钮的功能实现.将菜单中常用的功能放置在工具栏中,这样可以方便用户操作,省 ...