示例代码

  1. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  2. teamBlog.add(new Entry("first","My first blog entry."));
  3. eamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  4. XStream xstream = new XStream();
  5. System.out.println(xstream.toXML(teamBlog));

打印结果为:

  1. <xtream.Blog>
  2. <writer>
  3. <name>Guilherme Silveira</name>
  4. </writer>
  5. <entries>
  6. <xtream.Entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </xtream.Entry>
  10. <xtream.Entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </xtream.Entry>
  14. </entries>
  15. </xtream.Blog>

以上节点就包含了包名,如包名很长,就很难看了,怎样才能重新命名这个节点呀! 
以下的1,2设置效果一样

  1. 1 //xstream.aliasPackage("", "xtream");
  2. 2 xstream.alias("blog", Blog.class);
  3. 3 xstream.alias("entry", Entry.class);

通过这样设置就完成了节点名称的设置.如下:

  1. <blog>
  2. <writer>
  3. <name>Guilherme Silveira</name>
  4. </writer>
  5. <entries>
  6. <entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </entry>
  10. <entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </entry>
  14. </entries>
  15. </blog>

修改子节点属性名称 
将writer属性名称修改为:autor

  1. xstream.aliasField("author", Blog.class, "writer");

输出如下:

  1. <blog>
  2. <author>
  3. <name>Guilherme Silveira</name>
  4. </author>
  5. <entries>
  6. <entry>
  7. <title>first</title>
  8. <description>My first blog entry.</description>
  9. </entry>
  10. <entry>
  11. <title>tutorial</title>
  12. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  13. </entry>
  14. </entries>
  15. </blog>

删除集合节点名称的设置

  1. xstream.addImplicitCollection(Blog.class, "entries");

输出如下:

  1. <blog>
  2. <author>
  3. <name>Guilherme Silveira</name>
  4. </author>
  5. <entry>
  6. <title>first</title>
  7. <description>My first blog entry.</description>
  8. </entry>
  9. <entry>
  10. <title>tutorial</title>
  11. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  12. </entry>
  13. </blog>

将Blog类的元素设置为:它的节点属性 
//使用这个属性名作为节点上的元素

  1. xstream.useAttributeFor(Blog.class, "writer");

//重新命名

  1. xstream.aliasField("author", Blog.class, "writer");

//注册转换器

  1. xstream.registerConverter(new AuthorConverter());

转换器:

  1. import com.thoughtworks.xstream.converters.SingleValueConverter;
  2. class AuthorConverter implements SingleValueConverter {
  3. //显示节点属性值
  4. public String toString(Object obj) {
  5. return ((Author) obj).getName();
  6. }
  7. public Object fromString(String name) {
  8. return new Author(name);
  9. }
  10. public boolean canConvert(Class type) {
  11. return type.equals(Author.class);
  12. }
  13. }

显示结果:

  1. <blog author="Guilherme Silveira">
  2. <entry>
  3. <title>first</title>
  4. <description>My first blog entry.</description>
  5. </entry>
  6. <entry>
  7. <title>tutorial</title>
  8. <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
  9. </entry>
  10. </blog>

注解的使用方式,使用之前必须加上注解类才有作用:

  1. XStream xstream = new XStream();
  2. xstream.processAnnotations(Blog.class);
  3. xstream.processAnnotations(Entry.class);

1  @XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("blog", Blog.class);

  1. @XStreamAlias("blog")
  2. public class Blog {
  3. @XStreamAlias("author")
  4. private Author writer;
  5. .....
  6. }

当然Entry类也要设置同样的类属性:@XStreamAlias("entity") 
2 @XStreamImplicit去集合节点名:相当于 xstream.addImplicitCollection(Blog.class, "entries"); 
3 @XStreamConverter(AuthorConverter.class),参见以上的转换器类相当于:

  1. xstream.useAttributeFor(Blog.class, "writer");
  2. //重新命名
  3. xstream.aliasField("author", Blog.class, "writer");
  4. //注册转换器
  5. xstream.registerConverter(new AuthorConverter());

要使用这个注解AuthorConverter必须符合二个条件 
a 必须要有个默认的无参构造函数

  1. public AuthorConverter() {
  2. }

b 类声明必须为:public

  1. public class AuthorConverter implements SingleValueConverter {
  2. ...
  3. }

不用注解这二点都可以不强制要求! 
完整代码如下:

    1. import com.thoughtworks.xstream.XStream;
    2. import com.thoughtworks.xstream.annotations.XStreamAlias;
    3. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
    4. import com.thoughtworks.xstream.annotations.XStreamConverter;
    5. import com.thoughtworks.xstream.annotations.XStreamImplicit;
    6. @XStreamAlias("blog")
    7. public class Blog {
    8. @XStreamAsAttribute
    9. @XStreamAlias("author")
    10. @XStreamConverter(AuthorConverter.class)
    11. private Author writer;
    12. @XStreamImplicit
    13. private List entries = new ArrayList();
    14. public Blog(Author writer) {
    15. this.writer = writer;
    16. }
    17. public void add(Entry entry) {
    18. entries.add(entry);
    19. }
    20. public List getContent() {
    21. return entries;
    22. }
    23. public static void main(String[] args) {
    24. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
    25. teamBlog.add(new Entry("first", "My first blog entry."));
    26. teamBlog
    27. .add(new Entry("tutorial",
    28. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
    29. XStream xstream = new XStream();
    30. xstream.processAnnotations(Blog.class);
    31. xstream.processAnnotations(Entry.class);
    32. // 重新命名节点名
    33. // xstream.aliasPackage("", "xtream");
    34. /*
    35. * xstream.alias("blog", Blog.class); xstream.alias("entry",
    36. * Entry.class); //重新命名属性名 // xstream.aliasField("author", Blog.class,
    37. * "writer"); //去节点 xstream.addImplicitCollection(Blog.class,
    38. * "entries"); // xstream.useAttributeFor(Blog.class, "writer"); //
    39. * xstream.aliasField("author", Blog.class, "writer"); //
    40. * xstream.addImplicitCollection(Blog.class, "entries");
    41. * //使用这个属性名作为节点上的元素 xstream.useAttributeFor(Blog.class, "writer");
    42. * //重新命名 xstream.aliasField("author", Blog.class, "writer"); //注册转换器
    43. * xstream.registerConverter(new AuthorConverter());
    44. */
    45. System.out.println(xstream.toXML(teamBlog));
    46. }
    47. }

Xstream之常用方式与常用注解的更多相关文章

  1. 分方式缓存常用的一致性hash是什么原理

    分方式缓存常用的一致性hash是什么原理 一致性hash是用来解决什么问题的?先看一个场景有n个cache服务器,一个对象object映射到哪个cache上呢?可以采用通用方法计算object的has ...

  2. Windows校验文件哈希hash的两种常用方式

    大家经常都到哪儿去下载软件和应用程序呢?有没想过下载回来的软件.应用程序或资源是否安全呢?在 Windows 10 和 Office 2016 发布当初,很多没权限的朋友都使用第三方网站去下载安装映像 ...

  3. 操作xml文档的常用方式

    1.操作XML文档的两种常用方式: 1)使用XmlReader类和XmlWriter类操作 XmlReader是基于数据流的,占用极少的内存,是只读方式的,所以速度极快.只能采用遍历的模式查找数据节点 ...

  4. 查看Oracle SQL执行计划的常用方式

    在查看SQL执行计划的时候有很多方式 我常用的方式有三种 SQL> explain plan for 2 select * from scott.emp where ename='KING'; ...

  5. iOS应用数据存储的常用方式

    iOS应用 数据存储的常用方式 XML属性列表 plist Preference 偏好设置 NSKeyedArchiver 归档 Core Data SQLite3 应用沙盒: Layer:     ...

  6. Postman几种常用方式

    Postman几种常用方式 1.get请求直接拼URL形式 对于http接口,有get和post两种请求方式,当接口说明中未明确post中入参必须是json串时,均可用url方式请求 参数既可以写到U ...

  7. JS类继承常用方式发展史

    JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...

  8. 【转】shell:date 常用方式

    在linux下获取时间字符串 命令 date # 以yyyymmdd格式输出23天之前现在这个时刻的时间 $ date +%Y%m%d –date=’23 days ago’ $ date -u Th ...

  9. Ajax—web中ajax的常用方式

    什么Web2.0的特点? 1:注重用户贡献度 2:内容聚合RSS协议(每小块都个性化,单独加载单独请求,不用全部刷新--Ajax) 3:更丰富的用户体验 Ajax的概念? "Asynchro ...

随机推荐

  1. mysql的limit、order by和group by的用法

    程序执行会重复 用mysql很长时间,limit是分页的一个好工具, select * from table_a where num = 4 limit 1,10, select * from tab ...

  2. hibernate自动建表采用UTF-8字符编码

    hibernate自动建表采用UTF-8字符编码 hibernate建表默认为UTF-8编码 >>>>>>>>>>>>>& ...

  3. php跨服务器信息获取之cURL

    原文地址:php跨服务器信息获取之cURL作者:陌上花开 其实有几种方式 $content = file_get_contents("http://www.nettuts.com" ...

  4. css:nth-of-type()选择器用法

    今天做一个页面,无意中看到这个nth-of-type感觉挺方便的,之前单双行有的有横线,有的无横线一般在html中单独再写border-right:none等之类的.现在发现这个好东西赶紧记录下来. ...

  5. python 简明教程笔记

    1,python特点 python 注重的是如何解决问题,而不是语法和结构简单高效.扩展性 2,安装 python python -V        检测是否安装pythonctrl+d       ...

  6. 造一个Badge Service(徽章)的轮子

    什么是Badge Service 细心的读者朋友一定在很多Github的Repo,npm的package页面看到过诸如 的徽章.这些徽章是干什么用的? 大家看到上文中我引用的Badge的左侧,是Dow ...

  7. C#中的委托范例学习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  8. 使用DataList 分页方法

    什么是DataList我想应该不需要解释了,接下来分享本人在项目里使用到的通过DataList进行分页展示方法. 首先在ASPX页面添加一个DataList(后面都简称DL)控件,示例代码如下: &l ...

  9. c#equals相关

    1.==是直接比较值类型的值或引用类型的引用地址,但==不能用于struct,struct只能用equals来比较.==一般情况下与object.equals得到的结果是相等的. 2.Referenc ...

  10. 使用enable_shared_from_this示例

    /*测试enable_shared_from_this*/ #include <iostream> #include <boost/smart_ptr/shared_ptr.hpp& ...