五、使用别名(Alias)

首先,有这样一段Java代码:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import com.thoughtworks.xstream.XStream;
  4. public class XStreamTest2 {
  5. public static void main(String[] args) {
  6. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  7. teamBlog.add(new Entry("first","My first blog entry."));
  8. teamBlog.add(new Entry("tutorial",
  9. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  10. XStream xstream = new XStream();
  11. System.out.println(xstream.toXML(teamBlog));
  12. }
  13. }
  14. class Blog {
  15. private Author writer;
  16. private List entries = new ArrayList();
  17. public Blog(Author writer) {
  18. this.writer = writer;
  19. }
  20. public void add(Entry entry) {
  21. entries.add(entry);
  22. }
  23. public List getContent() {
  24. return entries;
  25. }
  26. }
  27. class Author {
  28. private String name;
  29. public Author(String name) {
  30. this.name = name;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. }
  36. class Entry {
  37. private String title, description;
  38. public Entry(String title, String description) {
  39. this.title = title;
  40. this.description = description;
  41. }
  42. }

对于上面这段代码,现在我要将一个Blog对象转换成为XML字符串,产生的结果是:

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

但是,我要将一个Blog对象转换成为下面的XML字符串的形式:

  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>
  9. Today we have developed a nice alias tutorial. Tell your friends! NOW!
  10. </description>
  11. </entry>
  12. </blog>

该怎么办呢?

这就需要用到别名转换方法了!

下面我们就一步步的调整代码:

1,给类起别名

需求:将节点cn.tjpu.zhw.xml.Blogcn.tjpu.zhw.xml.Entry重命名为blogentry

添加代码:

  1. xstream.alias("blog", Blog.class);
  2. xstream.alias("entry", Entry.class);

即main方法如下:

  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. System.out.println(xstream.toXML(teamBlog));
  10. }

运行结果如下:

  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>

2,给字段其别名

需求:将writer节点重命名为author节点

添加代码:

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

即main方法为:

  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. xstream.aliasField("author", Blog.class, "writer");
  10. System.out.println(xstream.toXML(teamBlog));
  11. }

运行结果如下:

  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>

3,使用隐式集合(Implicit Collection)

隐式集合:当你不想将一个集合的根节点呈现出来的时候,它就是一个隐式集合。

数组、Collection、Map都可以成为隐式集合。

需求:隐藏entries节点

添加代码:

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

则main方法如下:

  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. xstream.aliasField("author", Blog.class, "writer");
  10. xstream.addImplicitCollection(Blog.class, "entries");
  11. System.out.println(xstream.toXML(teamBlog));
  12. }

运行结果:

  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>

4,给XML属性起别名

需求:将writer节点,改成blog节点的authot属性

添加代码:

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

即main方法如下:

  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. xstream.alias("blog", Blog.class);
  8. xstream.alias("entry", Entry.class);
  9. xstream.useAttributeFor(Blog.class, "writer");
  10. xstream.aliasField("author", Blog.class, "writer");
  11. xstream.addImplicitCollection(Blog.class, "entries");
  12. System.out.println(xstream.toXML(teamBlog));
  13. }

但是运行的结果却是不是预想的那样:

  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>

可以看到,运行的结果根本就没有变化,为什么?

因为,还缺少一个转换器,XML节点的属性是一个String字符串,但是Author类不是字符串,这就需要一个转换器将Author对象转换成一个String对象、同时能够将String对象反转换为Author对象:

  1. //定义一个转换器,如果使用,则需要在使用时注册到对应的XStream对象中
  2. class AuthorConverter implements SingleValueConverter {
  3. /*该方法用于从Author对象中提取一个String字符串*/
  4. public String toString(Object obj) {
  5. return ((Author) obj).getName();
  6. }
  7. /*该方法用于从一个String字符串生成Author对象*/
  8. public Object fromString(String name) {
  9. return new Author(name);
  10. }
  11. /*该方法告诉XStream对象,该转换器可以转换哪种类型的对象*/
  12. public boolean canConvert(Class type) {
  13. return type.equals(Author.class);
  14. }
  15. }

新的main方法如下:

  1. public class XStreamTest2 {
  2. public static void main(String[] args) {
  3. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  4. teamBlog.add(new Entry("first","My first blog entry."));
  5. teamBlog.add(new Entry("tutorial",
  6. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  7. XStream xstream = new XStream();
  8. xstream.alias("blog", Blog.class);
  9. xstream.alias("entry", Entry.class);
  10. //设置XML节点的属性
  11. xstream.useAttributeFor(Blog.class, "writer");
  12. //注册转换器
  13. xstream.registerConverter(new AuthorConverter());
  14. xstream.aliasField("author", Blog.class, "writer");
  15. xstream.addImplicitCollection(Blog.class, "entries");
  16. System.out.println(xstream.toXML(teamBlog));
  17. }
  18. }

到此为止,运行的结果就会真正的向预期的那样:

  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>

5,使用包名别名

需求:有时候需要事项不同包里面的相同类名的节点之间的相互映射,这时就需要改变一下包名了

添加代码:

  1. xstream.aliasPackage("org.thoughtworks", "cn.tjpu.zhw.xml");

即更改main方法:

  1. public static void main(String[] args) {
  2. Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
  3. teamBlog.add(new Entry("first","My first blog entry."));
  4. teamBlog.add(new Entry("tutorial",
  5. "Today we have developed a nice alias tutorial. Tell your friends! NOW!"));
  6. XStream xstream = new XStream();
  7. //包名别名
  8. xstream.aliasPackage("org.thoughtworks", "cn.tjpu.zhw.xml");
  9. System.out.println(xstream.toXML(teamBlog));
  10. }

运行结果如下:

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

使用XStream是实现XML与Java对象的转换(2)--别名的更多相关文章

  1. 使用XStream是实现XML与Java对象的转换(6)--持久化

    九.持久化 在第八节的示例中,当我们操作一组对象时,我们可以指定Writer.OutputStream来写出序列化后的XML数据,我们还可以指定Reader.InputStream来读取序列化后的XM ...

  2. 使用XStream是实现XML与Java对象的转换(4)--转换器

    七.转换器(Converter) 我们程序中的POJO是千变万化的,而且需求也是千奇百怪的,所以XStream中的内置的转换器的功能不一定能够满足我们的要求,所以我们就需要自己构建转换器. 1,一个基 ...

  3. 使用XStream是实现XML与Java对象的转换(3)--注解

    六.使用注解(Annotation) 总是使用XStream对象的别名方法和注册转换器,会让人感到非常的乏味,又会产生很多重复性代码,于是我们可以使用注解的方式来配置要序列化的POJO对象. 1,最基 ...

  4. 使用XStream是实现XML与Java对象的转换(1)--简介及入门示例

    一.简单介绍 XStream是thoughtworks开发的开源框架,用于实现XML数据于Java对象.Json数据的转换.它不需要schema或其他的mapping文件就可以进行java对象和xml ...

  5. 使用XStream是实现XML与Java对象的转换(5)--Object Stream

    八,Object Stream 之前的例子我们都是直接输出Xml成为String类型或者从String中获得并解析Xml,现在我们要处理输入流和输出流! 1,输出流(ObjectOutputStrea ...

  6. 不规矩的xml与JAVA对象互相转换的小技巧-使用Marshaller

    摘要:将XML文档与JAVA对象互转是很常见的需求,如果XML定义很规整这很好实现.然而在现实中“不规矩”的XML可能更常见,Marshaller便无能为力了吗?下面是一个小技巧,调整一下思维便能重用 ...

  7. XStream轻松转换xml和java对象

    首先引入所需的jar: xstream-1.4.9.xpp3_min-1.1.4c.dom4j-1.6.1, 或用maven管理jar包时在pom.xml中添加: <!-- https://mv ...

  8. xml-mapping xml 与 java 对象转换映射框架,像 XStream 一样优雅地读写xml

    xml xml 是 java 实现的 xml 框架. 希望以最优雅的方式进行 xml 和 java 之间的转换处理,一行代码搞定一切. 特点 对象的和 xml 的互相映射 支持注解 @Alias 指定 ...

  9. XML 和 java对象相互转换

    XML 和 java对象相互转换 博客分类: XML 和 JSON   下面使用的是JDK自带的类,没有引用任何第三方jar包. Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 ...

随机推荐

  1. 5.0、Android Studio调试你的应用

    Android Studio包含了一个调试器,允许你调试运行在Android虚拟机或连接的Android设备的应用.通过Android Studio的调试器,你可以: 1. 选择你调试设备的app 2 ...

  2. Qzone React Native改造

    Android Qzone 6.1版本在情侣空间涉水React Native,以动态插件方式将情侣空间进行React Natived的改造.在情侣空间基础上,Android Qzone 6.2版本以融 ...

  3. Maven原型骨架及常见问题

    关于Maven原型工程的制作就不在这里详细介绍了,具体细节请参考之前的文章:定制Maven原型生成项目 下面分享制作和使用Maven原型工程时碰到的常见问题,以及原型的上传和使用方法. 1.模块路径问 ...

  4. NoSQL数据库之Redis数据库:Redis的介绍与安装部署

     NoSQL(NoSQL = Not Only SQL),它指的是非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的w ...

  5. (七十五)CoreLocation(一)在iOS7和iOS8设备上获取授权

    苹果在iOS8上更新了CoreLocation的授权获取方式,在原来的基础上,不仅需要调用授权函数,还需要对info.plist进行相应的配置. 在iOS上获取经纬度使用的是CoreLocationM ...

  6. 数据库隔离级别(mysql+Spring)与性能分析

     数据库隔离级别与Spring配置事务的联系及性能影响,以下是个人理解,如果有瑕疵请及时指正.   这里以mysql为例,先明确以下几个问题: 一.一般项目如果不自己配置事务的话,一般默认的是au ...

  7. java实现:将一个数逆序输出

    前面我们用C语言实现过这个程序,其实java也一样的,很多步骤跟C差不多,但是有些接口和特性可能不同: import java.util.Scanner;//要使用scanner这个类,就需要导入一个 ...

  8. 用过的一些Android设备调试特性注意点(挖坑帖)

    华为3C Activity切换动画偏快. 显示大图时不容易出现OOM(应用最大内容要比其他手机大一点),所以调试OOM问题时不要用此手机,否则难以发现问题. 小米3 不要调用系统的裁图功能.因为返回的 ...

  9. Socket编程实践(7) --Socket-Class封装(改进版v2)

    本篇博客定义一套用于TCP通信比较实用/好用Socket类库(运用C++封装的思想,将socket API尽量封装的好用与实用), 从开发出Socket库的第一个版本以来, 作者不知道做了多少改进,  ...

  10. 靠谱好用,ANDROID SQLITE 增删查改

    布局文件main实现简单的功能: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayo ...