在上一节中,我们已经看到了XStream是多么的简单易用,本文将继续以实例的方式讲解XStream别名。毕竟,你的JAVA对象不可能总是与XML结构毫发无差的,谁也不确定某个开发者手误打错了字母,或者是报文的名称发生了变化。
假设输出如下的XML结构,我们应该怎么做呢?
<blog author="一个木瓜">
<entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</entry>
<entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</entry>
</entries>
</blog>
1. 根据XML结构构建JAVA对象
package com.favccxx.favsoft.pojo;
import java.util.ArrayList;
import java.util.List;
public class Blog {
private Author writer;
private List<Entry> entries = new ArrayList<Entry>();
public Blog(Author writer) {
this.writer = writer;
}
public void add(Entry entry) {
entries.add(entry);
}
public void addEntry(Entry entry){
entries.add(entry);
}
public List<Entry> getContent() {
return entries;
}
public Author getWriter() {
return writer;
}
public void setWriter(Author writer) {
this.writer = writer;
}
}
package com.favccxx.favsoft.pojo;
public class Entry {
private String title;
private String description;
public Entry(String title, String description) {
this.title = title;
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
2.开始代码测试
package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
public static void main(String args[]) {
Blog myBlog = new Blog(new Author("一个木瓜"));
myBlog.add(new Entry("第一篇", "欢迎来到木瓜博客!"));
myBlog.add(new Entry("第二篇", "全国启动防雾霾红色预警。"));
XStream xstream = new XStream();
System.out.println(xstream.toXML(myBlog));
}
}
3.发现输出内容结构并不是想象的那样,怎么办?
<com.favccxx.favsoft.pojo.Blog>
<writer>
<name>一个木瓜</name>
</writer>
<entries>
<com.favccxx.favsoft.pojo.Entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</com.favccxx.favsoft.pojo.Entry>
<com.favccxx.favsoft.pojo.Entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</com.favccxx.favsoft.pojo.Entry>
</entries>
</com.favccxx.favsoft.pojo.Blog>
4.使用类别名,将包含路径的类对象进行替换。
xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);
发现输出结构与想要的结构近了一点,但还是不满足要求。
<blog>
<writer>
<name>一个木瓜</name>
</writer>
<entries>
<entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</entry>
<entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</entry>
</entries>
</blog>
5. 使用字段别名,将写手writer改成作者author,发现输出结构又近了一层。
xstream.aliasField("author", Blog.class, "writer");
<blog>
<author>
<name>一个木瓜</name>
</author>
<entries>
<entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</entry>
<entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</entry>
</entries>
</blog>
6. 使用隐式集合,将不需要展示的集合的根节点进行隐藏。需要注意的是数组和MAP结合不能声明成隐式集合。
xstream.addImplicitCollection(Blog.class, "entries");
<blog>
<author>
<name>一个木瓜</name>
</author>
<entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</entry>
<entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</entry>
</blog>
7. 使用属性别名,将xml中的成员对象以属性标签形式展示。但是属性标签并不会直接写到xml标签上去,需要实现SingleValueConverter转换器才行。
xstream.useAttributeFor(Blog.class, "writer");
xstream.aliasField("author", Blog.class, "writer");
package com.favccxx.favsoft.util;
import com.favccxx.favsoft.pojo.Author;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class AuthorConverter implements SingleValueConverter {
@Override
public boolean canConvert(Class type) {
return type.equals(Author.class);
}
@Override
public String toString(Object obj) {
return ((Author) obj).getName();
}
@Override
public Object fromString(String str) {
return new Author(str);
}
}
8.终于改完了,最终的完整代码是这样的
package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.favccxx.favsoft.util.AuthorConverter;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
public static void main(String args[]) {
Blog myBlog = new Blog(new Author("一个木瓜"));
myBlog.add(new Entry("第一篇", "欢迎来到木瓜博客!"));
myBlog.add(new Entry("第二篇", "全国启动防雾霾红色预警。"));
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.registerConverter(new AuthorConverter());
xstream.addImplicitCollection(Blog.class, "entries");
System.out.println(xstream.toXML(myBlog));
}
}
9.输出完美的XML结构
<blog author="一个木瓜">
<entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</entry>
<entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</entry>
</blog>
10.有时候需要使用包别名,将包名进行替换。需要替换的包名必须从首字母开始替换,不能从中间开始查找替换。
package com.favccxx.favsoft.main;
import com.favccxx.favsoft.pojo.Author;
import com.favccxx.favsoft.pojo.Blog;
import com.favccxx.favsoft.pojo.Entry;
import com.thoughtworks.xstream.XStream;
public class MainBlog {
public static void main(String args[]) {
Blog myBlog = new Blog(new Author("一个木瓜"));
myBlog.add(new Entry("第一篇", "欢迎来到木瓜博客!"));
myBlog.add(new Entry("第二篇", "全国启动防雾霾红色预警。"));
XStream xstream = new XStream();
xstream.aliasPackage("my.company", "com.favccxx");
System.out.println(xstream.toXML(myBlog));
}
}
11.输出格式如下
<my.company.favsoft.pojo.Blog>
<writer>
<name>一个木瓜</name>
</writer>
<entries>
<my.company.favsoft.pojo.Entry>
<title>第一篇</title>
<description>欢迎来到木瓜博客!</description>
</my.company.favsoft.pojo.Entry>
<my.company.favsoft.pojo.Entry>
<title>第二篇</title>
<description>全国启动防雾霾红色预警。</description>
</my.company.favsoft.pojo.Entry>
</entries>
</my.company.favsoft.pojo.Blog>
小结:
可以使用类别名改变标签的名称
可以使用字段别名改变标签的名称
可以使用包别名改变标签的名称
通过实现SingleValueConverter接口,可以将成员字段显示到对象属性标签上
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。