五、使用别名(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使用3”这个压缩包文件中,可能包含了使用XStream注解实现XML与Java对象转换的示例代码、测试用例以及相关的说明文档。通过对这些文件的深入学习,开发者可以更好地理解如何在实际项目中运用XStream的...
在`xmlAnalysis`文件夹中,可能包含了一个简单的Java程序,演示了如何使用DOM、SAX、StAX和XStream解析XML文件,并展示了XStream如何在JavaBean与XML之间进行转换。你可以运行这些代码,观察输出结果,以加深理解。...
这个"XStream测试Demo-xml与java对象的相互转换"是一个实例,展示了如何利用XStream进行这两种操作。在本文中,我们将深入探讨XStream的工作原理、使用方法以及其在实际开发中的应用。 首先,XStream的核心功能是...
在Java编程语言中,XStream库提供了一个简单且强大的机制,用于将Java对象序列化为XML,反之亦然。这使得在存储数据、传输数据或持久化对象时非常方便。本教程将深入探讨XStream库及其在Java中的使用,通过实际的...
xStream简化了Java对象与XML之间的转换,使得在处理XML数据时,开发者无需关注底层的XML构造细节。在Web Service接口对接时,如果需要传递XML内容,使用xStream可以高效、便捷地完成XML的拼接。通过合理配置和使用,...
在 XStream 1.4.9 版本中,开发者可以轻松地处理 XML 文档,将其转换为 Java 对象,或者将 Java 对象转换为 XML,用于数据存储、网络传输或配置文件等目的。 **XStream 的工作原理** XStream 的核心思想是通过元...
2. **从XML恢复对象**:使用`fromXML()`方法,可以将XML字符串转换回Java对象。 ```java MyBean restoredBean = (MyBean) xstream.fromXML(xml); ``` **XStream的安全性和注意事项:** 尽管XStream很方便,但请...
XStream通过映射XML节点到Java类的字段来实现对象的序列化和反序列化。这意味着你需要定义一个Java类,该类的字段对应于XML文档中的元素。XStream会自动处理这个映射过程,无需编写复杂的DOM或SAX解析代码。 下面是...
如果XML结构与Java类不完全匹配,可以通过XStream的alias方法自定义别名,或者使用annotations进行更精确的映射。 在提供的`AppDemo`文件中,可能包含了一个简单的示例应用,演示了如何在实际项目中使用XStream进行...
`XStream` 是一个强大的Java库,它允许我们将Java对象序列化为XML,反之亦然,极大地简化了XML与Java对象之间的转换过程。下面我们将详细探讨`XStream`库的工作原理以及如何使用它来实现XML和Java对象的互换。 首先...
在这个场景中,我们将探讨如何使用XStream将XML文档转换成Java对象,进而转化为JSON对象。 首先,我们需要引入XStream库。XStream的核心功能是能够将Java对象和XML之间的映射自动化,极大地简化了序列化和反序列化...
2. **对象与XML的映射**:在XStream中,每个Java类可以映射到一个XML元素,类的字段则映射到XML的属性或子元素。例如,有一个`Person`类: ```java public class Person { private String name; private int age...
但是当对象和字段名与XML中的元素名不同时,XStream支持指定别名。XStream支持以方法调用的方式,或是Java 标注的方式指定别名。 XStream在进行数据类型转换时,使用系统缺省的类型转换器。同时,也支持用户自定义...
XStream是一款强大的Java库,主要用于实现Java对象与XML文档之间的相互转换。本文将详细介绍XStream的基本用法、配置选项以及如何利用它来进行对象到XML的序列化和反序列化操作。 #### 一、XStream简介 XStream是...
标题中的“XStream实现Object与XML转换解决方案”指的是一个Java库——XStream,它提供了一种简单的方法来序列化和反序列化Java对象到XML,反之亦然。这个库广泛用于将程序数据保存到XML文件或者从XML数据恢复对象,...
XStream不仅能够将Java对象转换为XML,还可以反向将XML转换回Java对象,极大地简化了对象与XML之间的序列化和反序列化过程。 XStream库是由Johannes Lehtinen创建的一个开源项目,它是基于Java的,可以处理复杂的...
**XStream:JavaBean与XML/JSON之间的转换大师** XStream是一个开源库,它为Java对象提供了简单且直观的XML序列化和反序列化的解决方案。它不仅能够将Java对象转换成XML,反之亦然,还能支持JSON格式的转换。这个...
Xstream是一款强大的Java库,用于实现Java对象与XML文档之间的转换。这种转换不仅支持基本数据类型,也适用于复杂的数据结构,包括自定义类和集合。Xstream的设计旨在简化序列化过程,使得开发者能够轻松地在Java...
**XStream XML解析** ...总之,XStream 提供了一种高效且直观的 Java 对象与 XML 之间的转换方法,尤其适用于那些需要频繁进行对象序列化的项目。理解和掌握 XStream 的使用,可以极大地提升开发效率并简化代码维护。
【xStream 框架详解:Java对象与XML、JSON的完美转换】 xStream是一个强大的Java库,它提供了简单易用的API,用于将Java对象序列化为XML,以及将XML反序列化回Java对象。此外,xStream还支持JSON格式的转换。这个...