在上一节中,我们已经看到了XStream是多么的简单易用,本文将继续以实例的方式讲解XStream别名。毕竟,你的JAVA对象不可能总是与XML结构毫发无差的,谁也不确定某个开发者手误打错了字母,或者是报文的名称发生了变化。

假设输出如下的XML结构,我们应该怎么做呢?
第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
1. 根据XML结构构建JAVA对象
package com.favccxx.favsoft.pojo;
import java.util.ArrayList;
import java.util.List;
public 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 void addEntry(Entry entry){
entries.add(entry);
}
public List 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.发现输出内容结构并不是想象的那样,怎么办?
一个木瓜 第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
4.使用类别名,将包含路径的类对象进行替换。
xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);发现输出结构与想要的结构近了一点,但还是不满足要求。
一个木瓜 第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
5. 使用字段别名,将写手writer改成作者author,发现输出结构又近了一层。
xstream.aliasField("author", Blog.class, "writer");一个木瓜 第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
6. 使用隐式集合,将不需要展示的集合的根节点进行隐藏。需要注意的是数组和MAP结合不能声明成隐式集合。
xstream.addImplicitCollection(Blog.class, "entries");
一个木瓜 第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
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结构
第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
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.输出格式如下
一个木瓜 第一篇 欢迎来到木瓜博客! 第二篇 全国启动防雾霾红色预警。
小结:
可以使用类别名改变标签的名称
可以使用字段别名改变标签的名称
可以使用包别名改变标签的名称
通过实现SingleValueConverter接口,可以将成员字段显示到对象属性标签上
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:详解XStream别名-创新互联
转载来于:http://www.lzwzjz.cn/article/hsdog.html


咨询
建站咨询
