xstream中几个注解的含义和用法
2013-01-02 23:31
295 查看
XStream是个很强大的工具,能将java对象和xml之间相互转化。xstream不在意java类中成员变量是私有还是公有,也不在乎是否有默认构造函数。它调用方式也非常简单:从xml对象转化为java对象,使用fromXML()方法;从java对象序列化为xml,toXML()即可,很方便。xstream也支持注解方式,这些都是为了简化输出而设计,下面为大家简单说一下这几个注解的含义和用法。
1. 当没有任何注解情况下
其中Ball定义如下:
没有任何注解,输出如下:
1. @XStreamAlias("cat") 等同于 xstream.alias("cat", Cat.class);
我们需要明确给出,哪个类的注解需要被激活:
2. XStreamAsAttribute 作用是将类内成员作为父节点属性输出,等同于xstream.useAttributeFor(Cat.class, "name")
3. @XStreamAlias 作用就是将属性按照别名输出,等同于xstream.aliasField("catAge", Cat.class, "age");
4.对于集合常用的注解 @XStreamImplicit 去除<Balls></Balls>显示,只显示之间的<Ball></Ball>节点元素
此时输出如下:
4.还有几个注解,都是比较好理解的,如@XStreamOmitField 表明该属性不会被序列化到xml中。
常用的就这么几个,其他的再讨论和学习吧。
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); }得到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;
常用的就这么几个,其他的再讨论和学习吧。
相关文章推荐
- SpringMVC实现依赖注入的几个常用注解的用法解释和示例说明
- Spring 几个配置注解的含义
- JUnit 单元测试几个方法的用法注解
- SpringMVC实现依赖注入的几个常用注解的用法解释和示例说明
- @XStreamAlias注解的用法。
- flask_script的几个具体用法
- ######Spring第5天ssh整合(注解版)难理解知识点:【自定义 方法加注解的方式】给dao注入sessionFactory属性+【搞清@Autowared和@Resource含义】
- Promise对象的含义和基本用法
- Retrofit各个注解的含义及作用
- JS产生随机数的几个用法!
- @Query注解的用法(Spring Data JPA)
- @SuppressWarnings注解用法详解(转)
- UML类图中箭头和线条的含义和用法
- Oracle中decode函数用法含义
- spring的@Autowired 和@Resource注解的用法和区别
- Java Android 注解(Annotation) 及几个常用开源项目注解原理简析
- Flex如何创建状态States并掌握几个常用控件用法
- Makefile学习笔记<中一些特殊符号的含义和用法>
- this指针的含义及其用法(转自飞诺网开发开发频道)
- @RequestMapping注解的用法