使用XStream是实现XML与Java对象的转换(2)--别名
五、使用别名(Alias)
首先,有这样一段Java代码:
- import java.util.ArrayList;
- import java.util.List;
- import com.thoughtworks.xstream.XStream;
- public class XStreamTest2 {
- 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();
- System.out.println(xstream.toXML(teamBlog));
- }
- }
- class Blog {
- private Author writer;
- 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;
- }
- }
- class Author {
- private String name;
- public Author(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- }
- class Entry {
- private String title, description;
- public Entry(String title, String description) {
- this.title = title;
- this.description = description;
- }
- }
对于上面这段代码,现在我要将一个Blog对象转换成为XML字符串,产生的结果是:
- <cn.tjpu.zhw.xml.Blog>
- <writer>
- <name>Guilherme Silveira</name>
- </writer>
- <entries>
- <cn.tjpu.zhw.xml.Entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </cn.tjpu.zhw.xml.Entry>
- <cn.tjpu.zhw.xml.Entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </cn.tjpu.zhw.xml.Entry>
- </entries>
- </cn.tjpu.zhw.xml.Blog>
但是,我要将一个Blog对象转换成为下面的XML字符串的形式:
- <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>
该怎么办呢?
这就需要用到别名转换方法了!
下面我们就一步步的调整代码:
1,给类起别名
需求:将节点cn.tjpu.zhw.xml.Blog、cn.tjpu.zhw.xml.Entry重命名为blog和entry
添加代码:
- xstream.alias("blog", Blog.class);
- xstream.alias("entry", Entry.class);
即main方法如下:
- 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.alias("blog", Blog.class);
- xstream.alias("entry", Entry.class);
- System.out.println(xstream.toXML(teamBlog));
- }
运行结果如下:
- <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>
2,给字段其别名
需求:将writer节点重命名为author节点
添加代码:
- xstream.aliasField("author", Blog.class, "writer");
即main方法为:
- 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.alias("blog", Blog.class);
- xstream.alias("entry", Entry.class);
- xstream.aliasField("author", Blog.class, "writer");
- System.out.println(xstream.toXML(teamBlog));
- }
运行结果如下:
- <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>
3,使用隐式集合(Implicit Collection)
隐式集合:当你不想将一个集合的根节点呈现出来的时候,它就是一个隐式集合。
数组、Collection、Map都可以成为隐式集合。
需求:隐藏entries节点
添加代码:
- xstream.addImplicitCollection(Blog.class, "entries");
则main方法如下:
- 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.alias("blog", Blog.class);
- xstream.alias("entry", Entry.class);
- xstream.aliasField("author", Blog.class, "writer");
- xstream.addImplicitCollection(Blog.class, "entries");
- System.out.println(xstream.toXML(teamBlog));
- }
运行结果:
- <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>
4,给XML属性起别名
需求:将writer节点,改成blog节点的authot属性
添加代码:
- xstream.useAttributeFor(Blog.class, "writer");
即main方法如下:
- 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.alias("blog", Blog.class);
- xstream.alias("entry", Entry.class);
- xstream.useAttributeFor(Blog.class, "writer");
- xstream.aliasField("author", Blog.class, "writer");
- xstream.addImplicitCollection(Blog.class, "entries");
- System.out.println(xstream.toXML(teamBlog));
- }
但是运行的结果却是不是预想的那样:
- <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>
可以看到,运行的结果根本就没有变化,为什么?
因为,还缺少一个转换器,XML节点的属性是一个String字符串,但是Author类不是字符串,这就需要一个转换器将Author对象转换成一个String对象、同时能够将String对象反转换为Author对象:
- //定义一个转换器,如果使用,则需要在使用时注册到对应的XStream对象中
- class AuthorConverter implements SingleValueConverter {
- /*该方法用于从Author对象中提取一个String字符串*/
- public String toString(Object obj) {
- return ((Author) obj).getName();
- }
- /*该方法用于从一个String字符串生成Author对象*/
- public Object fromString(String name) {
- return new Author(name);
- }
- /*该方法告诉XStream对象,该转换器可以转换哪种类型的对象*/
- public boolean canConvert(Class type) {
- return type.equals(Author.class);
- }
- }
新的main方法如下:
- public class XStreamTest2 {
- 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.alias("blog", Blog.class);
- xstream.alias("entry", Entry.class);
- //设置XML节点的属性
- xstream.useAttributeFor(Blog.class, "writer");
- //注册转换器
- xstream.registerConverter(new AuthorConverter());
- xstream.aliasField("author", Blog.class, "writer");
- xstream.addImplicitCollection(Blog.class, "entries");
- System.out.println(xstream.toXML(teamBlog));
- }
- }
到此为止,运行的结果就会真正的向预期的那样:
- <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>
5,使用包名别名
需求:有时候需要事项不同包里面的相同类名的节点之间的相互映射,这时就需要改变一下包名了
添加代码:
- xstream.aliasPackage("org.thoughtworks", "cn.tjpu.zhw.xml");
即更改main方法:
- 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.aliasPackage("org.thoughtworks", "cn.tjpu.zhw.xml");
- System.out.println(xstream.toXML(teamBlog));
- }
运行结果如下:
- <org.thoughtworks.Blog>
- <writer>
- <name>Guilherme Silveira</name>
- </writer>
- <entries>
- <org.thoughtworks.Entry>
- <title>first</title>
- <description>My first blog entry.</description>
- </org.thoughtworks.Entry>
- <org.thoughtworks.Entry>
- <title>tutorial</title>
- <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
- </org.thoughtworks.Entry>
- </entries>
- </org.thoughtworks.Blog>
使用XStream是实现XML与Java对象的转换(2)--别名的更多相关文章
- 使用XStream是实现XML与Java对象的转换(6)--持久化
九.持久化 在第八节的示例中,当我们操作一组对象时,我们可以指定Writer.OutputStream来写出序列化后的XML数据,我们还可以指定Reader.InputStream来读取序列化后的XM ...
- 使用XStream是实现XML与Java对象的转换(4)--转换器
七.转换器(Converter) 我们程序中的POJO是千变万化的,而且需求也是千奇百怪的,所以XStream中的内置的转换器的功能不一定能够满足我们的要求,所以我们就需要自己构建转换器. 1,一个基 ...
- 使用XStream是实现XML与Java对象的转换(3)--注解
六.使用注解(Annotation) 总是使用XStream对象的别名方法和注册转换器,会让人感到非常的乏味,又会产生很多重复性代码,于是我们可以使用注解的方式来配置要序列化的POJO对象. 1,最基 ...
- 使用XStream是实现XML与Java对象的转换(1)--简介及入门示例
一.简单介绍 XStream是thoughtworks开发的开源框架,用于实现XML数据于Java对象.Json数据的转换.它不需要schema或其他的mapping文件就可以进行java对象和xml ...
- 使用XStream是实现XML与Java对象的转换(5)--Object Stream
八,Object Stream 之前的例子我们都是直接输出Xml成为String类型或者从String中获得并解析Xml,现在我们要处理输入流和输出流! 1,输出流(ObjectOutputStrea ...
- 不规矩的xml与JAVA对象互相转换的小技巧-使用Marshaller
摘要:将XML文档与JAVA对象互转是很常见的需求,如果XML定义很规整这很好实现.然而在现实中“不规矩”的XML可能更常见,Marshaller便无能为力了吗?下面是一个小技巧,调整一下思维便能重用 ...
- XStream轻松转换xml和java对象
首先引入所需的jar: xstream-1.4.9.xpp3_min-1.1.4c.dom4j-1.6.1, 或用maven管理jar包时在pom.xml中添加: <!-- https://mv ...
- xml-mapping xml 与 java 对象转换映射框架,像 XStream 一样优雅地读写xml
xml xml 是 java 实现的 xml 框架. 希望以最优雅的方式进行 xml 和 java 之间的转换处理,一行代码搞定一切. 特点 对象的和 xml 的互相映射 支持注解 @Alias 指定 ...
- XML 和 java对象相互转换
XML 和 java对象相互转换 博客分类: XML 和 JSON 下面使用的是JDK自带的类,没有引用任何第三方jar包. Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 ...
随机推荐
- 1.2、Android Studio为新设备创建一个模块
模块为你的应用的源码.资源文件和app level设置(比如AndroidManifest.xml)提供了一个容器.每个模块可以独立的构建.测试和调试. 通过使用模块,Android Studio可以 ...
- wincvs的“License for this product has expired”问题解决
新入职的公司代码管理工具是CVS,使用wincvs作为客户端工具.今天发现执行login.logout.update等操作的时候总是报"License for this product ha ...
- Uva - Uva272 - TEX Quotes
TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few ty ...
- Intellij IDEA插件开发入门
现今的IDE尽管有如"洪水猛兽"般强大,但要知道再强大的IDE也没法提供给使用者想要的一切功能, 所以IDE一般都提供有API接口供开发者自行扩展.下面以Intellij IDEA ...
- JQuery实战---初识JQuery+入门实例
JQuery在小编的世界中,也就是JavaScript和查询(Query),即是辅助JavaScript开发的库,百度百科对JQuery的介绍比较详细,小伙伴可以东东自己可耐的小爪子,上网进行搜索,说 ...
- Hessian源码分析--HessianProxy
在上一篇博客 Hessian源码分析--HessianProxyFactory 中我们了解到,客户端获得的对象其实是HessianProxy生成的目标对象,当调用目标对象的方法时,会调用Hessian ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Shell入门之概念
1.一切皆是文件: 在bash Shell 中一切皆是文件,不管是我们认为的文本文件,还是那些文件夹的东西,在这里都是文件,Linux只管比特和字节流,而不关心他们最终组成了什么格式,这些工作交给在L ...
- Linux进程实践(3) --进程终止与exec函数族
进程的几种终止方式 (1)正常退出 从main函数返回[return] 调用exit 调用_exit/_Exit (2)异常退出 调用abort 产生SIGABOUT信号 由信号终止 Ctrl+ ...
- 小强的HTML5移动开发之路(8)——坦克大战游戏2
来自:http://blog.csdn.net/cai_xingyun/article/details/48629015 在上一篇文章中我们已经画出了自己的坦克,并且可以控制自己的坦克移动,我们继续接 ...