Xstream之常用方式与常用注解
示例代码
- Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
- teamBlog.add(new Entry("first","My first blog entry."));
- eamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
- XStream xstream = new XStream();
- System.out.println(xstream.toXML(teamBlog));
打印结果为:
- <xtream.Blog>
- <writer>
- <name>Guilherme Silveira</name>
- </writer>
- <entries>
- <xtream.Entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </xtream.Entry>
- <xtream.Entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </xtream.Entry>
- </entries>
- </xtream.Blog>
以上节点就包含了包名,如包名很长,就很难看了,怎样才能重新命名这个节点呀!
以下的1,2设置效果一样
- 1 //xstream.aliasPackage("", "xtream");
- 2 xstream.alias("blog", Blog.class);
- 3 xstream.alias("entry", Entry.class);
通过这样设置就完成了节点名称的设置.如下:
- <blog>
- <writer>
- <name>Guilherme Silveira</name>
- </writer>
- <entries>
- <entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </entry>
- <entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </entry>
- </entries>
- </blog>
修改子节点属性名称
将writer属性名称修改为:autor
- xstream.aliasField("author", Blog.class, "writer");
输出如下:
- <blog>
- <author>
- <name>Guilherme Silveira</name>
- </author>
- <entries>
- <entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </entry>
- <entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </entry>
- </entries>
- </blog>
删除集合节点名称的设置
- xstream.addImplicitCollection(Blog.class, "entries");
输出如下:
- <blog>
- <author>
- <name>Guilherme Silveira</name>
- </author>
- <entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </entry>
- <entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </entry>
- </blog>
将Blog类的元素设置为:它的节点属性
//使用这个属性名作为节点上的元素
- xstream.useAttributeFor(Blog.class, "writer");
//重新命名
- xstream.aliasField("author", Blog.class, "writer");
//注册转换器
- xstream.registerConverter(new AuthorConverter());
转换器:
- import com.thoughtworks.xstream.converters.SingleValueConverter;
- class AuthorConverter implements SingleValueConverter {
- //显示节点属性值
- public String toString(Object obj) {
- return ((Author) obj).getName();
- }
- public Object fromString(String name) {
- return new Author(name);
- }
- public boolean canConvert(Class type) {
- return type.equals(Author.class);
- }
- }
显示结果:
- <blog author="Guilherme Silveira">
- <entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </entry>
- <entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </entry>
- </blog>
注解的使用方式,使用之前必须加上注解类才有作用:
- XStream xstream = new XStream();
- xstream.processAnnotations(Blog.class);
- xstream.processAnnotations(Entry.class);
1 @XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("blog", Blog.class);
- @XStreamAlias("blog")
- public class Blog {
- @XStreamAlias("author")
- private Author writer;
- .....
- }
当然Entry类也要设置同样的类属性:@XStreamAlias("entity")
2 @XStreamImplicit去集合节点名:相当于 xstream.addImplicitCollection(Blog.class, "entries");
3 @XStreamConverter(AuthorConverter.class),参见以上的转换器类相当于:
- xstream.useAttributeFor(Blog.class, "writer");
- //重新命名
- xstream.aliasField("author", Blog.class, "writer");
- //注册转换器
- xstream.registerConverter(new AuthorConverter());
要使用这个注解AuthorConverter必须符合二个条件
a 必须要有个默认的无参构造函数
- public AuthorConverter() {
- }
b 类声明必须为:public
- public class AuthorConverter implements SingleValueConverter {
- ...
- }
不用注解这二点都可以不强制要求!
完整代码如下:
- import com.thoughtworks.xstream.XStream;
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
- import com.thoughtworks.xstream.annotations.XStreamConverter;
- import com.thoughtworks.xstream.annotations.XStreamImplicit;
- @XStreamAlias("blog")
- public class Blog {
- @XStreamAsAttribute
- @XStreamAlias("author")
- @XStreamConverter(AuthorConverter.class)
- private Author writer;
- @XStreamImplicit
- private List entries = new ArrayList();
- public Blog(Author writer) {
- this.writer = writer;
- }
- public void add(Entry entry) {
- entries.add(entry);
- }
- public List getContent() {
- return entries;
- }
- public static void main(String[] args) {
- Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
- teamBlog.add(new Entry("first", "My first blog entry."));
- teamBlog
- .add(new Entry("tutorial",
- "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
- XStream xstream = new XStream();
- xstream.processAnnotations(Blog.class);
- xstream.processAnnotations(Entry.class);
- // 重新命名节点名
- // xstream.aliasPackage("", "xtream");
- /*
- * xstream.alias("blog", Blog.class); xstream.alias("entry",
- * Entry.class); //重新命名属性名 // xstream.aliasField("author", Blog.class,
- * "writer"); //去节点 xstream.addImplicitCollection(Blog.class,
- * "entries"); // xstream.useAttributeFor(Blog.class, "writer"); //
- * xstream.aliasField("author", Blog.class, "writer"); //
- * xstream.addImplicitCollection(Blog.class, "entries");
- * //使用这个属性名作为节点上的元素 xstream.useAttributeFor(Blog.class, "writer");
- * //重新命名 xstream.aliasField("author", Blog.class, "writer"); //注册转换器
- * xstream.registerConverter(new AuthorConverter());
- */
- System.out.println(xstream.toXML(teamBlog));
- }
- }
Xstream之常用方式与常用注解的更多相关文章
- 分方式缓存常用的一致性hash是什么原理
分方式缓存常用的一致性hash是什么原理 一致性hash是用来解决什么问题的?先看一个场景有n个cache服务器,一个对象object映射到哪个cache上呢?可以采用通用方法计算object的has ...
- Windows校验文件哈希hash的两种常用方式
大家经常都到哪儿去下载软件和应用程序呢?有没想过下载回来的软件.应用程序或资源是否安全呢?在 Windows 10 和 Office 2016 发布当初,很多没权限的朋友都使用第三方网站去下载安装映像 ...
- 操作xml文档的常用方式
1.操作XML文档的两种常用方式: 1)使用XmlReader类和XmlWriter类操作 XmlReader是基于数据流的,占用极少的内存,是只读方式的,所以速度极快.只能采用遍历的模式查找数据节点 ...
- 查看Oracle SQL执行计划的常用方式
在查看SQL执行计划的时候有很多方式 我常用的方式有三种 SQL> explain plan for 2 select * from scott.emp where ename='KING'; ...
- iOS应用数据存储的常用方式
iOS应用 数据存储的常用方式 XML属性列表 plist Preference 偏好设置 NSKeyedArchiver 归档 Core Data SQLite3 应用沙盒: Layer: ...
- Postman几种常用方式
Postman几种常用方式 1.get请求直接拼URL形式 对于http接口,有get和post两种请求方式,当接口说明中未明确post中入参必须是json串时,均可用url方式请求 参数既可以写到U ...
- JS类继承常用方式发展史
JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...
- 【转】shell:date 常用方式
在linux下获取时间字符串 命令 date # 以yyyymmdd格式输出23天之前现在这个时刻的时间 $ date +%Y%m%d –date=’23 days ago’ $ date -u Th ...
- Ajax—web中ajax的常用方式
什么Web2.0的特点? 1:注重用户贡献度 2:内容聚合RSS协议(每小块都个性化,单独加载单独请求,不用全部刷新--Ajax) 3:更丰富的用户体验 Ajax的概念? "Asynchro ...
随机推荐
- JSPatch 成长之路
在这次 GMTC 大会上,我见到了 JSPatch 的作者 bang.在这之前我就和他在网上认识并聊过很多次,bang 也在这个公众号上投稿发表了多篇关于 JSPatch 的文章,包括:JSPatch ...
- hdu2091JAVA
空心三角形 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- select/**/*/**/from/**/RegSite
select/**/*/**/from/**/RegSite 这样写sql也是可以的 替换空格
- 对java框架的几点认识
java框架实在是太多了,网上一抄一大段,根本就了解不到什么.我还是以我的经验来说一下j2ee的框架.1.首先力推struts2框架,这是最经典的框架(可以说没有“之一”).可以帮你快速搭建出一个MV ...
- [功能帮助类] 最新的Functions 类 (转载)
代码 using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptogr ...
- Android Studio SDK Manager无法正常下载如何设置
博客分类: Linux 零散小知识 Android那点事 AndroidStudioSDKManager 一方面在/etc/hosts中设置: #Google主页 203.208.46.146 ww ...
- 写一个最简单的 Server
import java.net.*;import java.io.*;public class Server{ public static void main(String[] args) throw ...
- C#和SQL操作Xml
#region DataTableToXml public static string DataTableToXml(System.Data.DataTable Dt) { ...
- Android - IOExceptionConnection to xxx refused.
还是stackoverflow上老外牛,往google上type一下,就找到原因了. 今天在使用Apache提供的HttpClient连接Tomcat服务器,使用log捕获异常的时候,提示说:IOEx ...
- 基于shiro授权过程
1.对subject进行授权,调用方法isPermitted("permission串")2.SecurityManager执行授权,通过ModularRealmAuthorize ...