五、使用别名(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>
相关推荐
NULL 博文链接:https://forestqqqq.iteye.com/blog/1996095
NULL 博文链接:https://forestqqqq.iteye.com/blog/1995092
NULL 博文链接:https://forestqqqq.iteye.com/blog/1993210
xstream轻松解析xml到java对象,内附样例。so easy!妈妈再也不用担心我的xml解析。
NULL 博文链接:https://forestqqqq.iteye.com/blog/1996974
使用 XStream 不用任何映射就能实现多数 Java 对象的序列化。在生成的 XML 中对象名变成了元素名,类中的字符串组成了 XML 中的元素内容。使用 XStream 序列化的类不需要实现 Serializable 接口。XStream 是一种序列...
有jar包。在附件。全的,代码也有,,直接运行
XStream测试Demo-xml与java对象的相互转换
好用的xStream XML和JAVA对象互相转换的简单示例,内附依赖的xstream-1.3.1.jar和xpp3_min-1.1.4c.jar
使用xstream进行java类与xml文件互转解析
它可以将Java对象转换为XML文档,也可以将XML文档转换为Java对象。 pom引用 <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream <version>1.4.20 实体类和测试类详见博客 ...
xStream完美转换XML、JSON,包括XML转换成对象,以及对象转换成XML,以及对象转换成JSON,以及注意事项
一个小demo展示xml的两种解析方式和四种解析方法,以及使用XStream进行javaBean与xml、json之间的转换
但是当对象和字段名与XML中的元素名不同时,XStream支持指定别名。XStream支持以方法调用的方式,或是Java 标注的方式指定别名。 XStream在进行数据类型转换时,使用系统缺省的类型转换器。同时,也支持用户自定义...
Xstream_java对象和xml的互相转换
java中对象与xml转换,实例带完整源码
xstream-1.4.9实现xml与对象互转,不会用可以找dddddddddddddddddd