XStream指定别名

XStream Alias

总结要达到的效果需要的Model简单测试类别名字段别名隐式集合属性别名包别名

 

总结

主要如下:

    • 可以使用类的别名来改变标签名称
    • 可以使用字段的别名来改变标签名称
    • 可以使用包别名来改变标签名称
    • 如果字段类型是由一个SingleValueConverter处理,可写为属性

 

要达到的效果

使用XStream生成如下格式的XML

<blogauthor="张三">

 <entry>

   <title>文章1</title>

   <description>这是文章1</description>

 </entry>

 <entry>

   <title>文章2</title>

   <description>

       这是文章2

   </description>

 </entry>

</blog>

 

需要的Model

Blog

publicclass Blog {

private Authorwriter;

private List<Entry>entries = new ArrayList<>();

 

public Blog(Author writer) {this.writer = writer;}

public Author getWriter() {returnwriter;}

publicvoid setWriter(Author writer) {this.writer = writer;}

public List<Entry> getEntries() {returnentries;}

publicvoid setEntries(List<Entry> entries) {this.entries = entries;}

 

publicvoid add(Entry entry) {entries.add(entry);}

public List<Entry> getContent() {returnentries;}

}

Author.java

publicclass Author {

private Stringname;

public Author(String name) {this.name = name;}

public String getName() {returnname;}

publicvoid setName(String name) {this.name = name;}

}

Entry.java

publicclass Entry {

private Stringtitle;

private Stringdescription;

 

public Entry(String title, String description) {

this.title = title;

this.description = description;

}

 

public String getTitle() {returntitle;}

publicvoid setTitle(String title) {this.title = title;}

public String getDescription() {returndescription;}

publicvoid setDescription(String description) {this.description = description;}

}

 

简单测试

创建Blog类实例,并使用XStream生成XML

publicclass XStreamTest {

publicstaticvoid main(String[] args) {

Blog teamBlog =new Blog(new Author("张三"));

teamBlog.add(new Entry("文章1","这是文章1"));

teamBlog.add(new Entry("文章2","这是文章2"));

 

XStream xstream = new XStream();

System.out.println(xstream.toXML(teamBlog));

}

}

生成的格式如下:

<xstream.Blog>

 <writer>

   <name>张三</name>

 </writer>

 <entries>

   <xstream.Entry>

     <title>文章1</title>

     <description>这是文章1</description>

   </xstream.Entry>

   <xstream.Entry>

     <title>文章2</title>

     <description>这是文章2</description>

   </xstream.Entry>

 </entries>

</xstream.Blog>

 

类别名

改变标签名xstream.Blogblogxstream.Entryentry

xstream.alias("blog", Blog.class);

xstream.alias("entry", Entry.class);

结果现在变成了:

<blog>

 <writer>

   <name>张三</name>

 </writer>

 <entries>

   <entry>

     <title>文章1</title>

     <description>这是文章1</description>

   </entry>

   <entry>

     <title>文章2</title>

     <description>这是文章2</description>

   </entry>

 </entries>

</blog>

 

字段别名

改变标签writer的名字,但这次要使用一个字段的别名:

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

现在结果变成了这样:

<blog>

 <author>

   <name>张三</name>

 </author>

 <entries>

   <entry>

     <title>文章1</title>

     <description>这是文章1</description>

   </entry>

   <entry>

     <title>文章2</title>

     <description>这是文章2</description>

   </entry>

 </entries>

</blog>

 

隐式集合

如果不需要显示 entries标签,可通过addImplicitCollection方法不让其显示:

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

现在的结果:

<blog>

 <author>

   <name>张三</name>

 </author>

 <entry>

   <title>文章1</title>

   <description>这是文章1</description>

 </entry>

 <entry>

   <title>文章2</title>

   <description>这是文章2</description>

 </entry>

</blog>

 

属性别名

writer 成员变量设置为一个XML 属性。

告诉XStream,其中Blog类的 author字段将做为一个author属性:

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

实现SingleValueConverter

publicclass AuthorConverterimplements SingleValueConverter { 

@Override

publicboolean canConvert(Class type) {

return type.equals(Author.class);

}

 

@Override

public Object fromString(String name) {

returnnew Author(name);

}

 

@Override

public String toString(Object obj) {

return ((Author) obj).getName();

}

}

注册这个转换器:AuthorConverter

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

xstream.registerConverter(new AuthorConverter());

完整代码:

publicclass XStreamTest {

publicstaticvoid main(String[] args) {

Blog teamBlog =new Blog(new Author("张三"));

teamBlog.add(new Entry("文章1","这是文章1"));

teamBlog.add(new Entry("文章2","这是文章2"));

 

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");

 

//属性别名

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

xstream.registerConverter(new AuthorConverter());

 

System.out.println(xstream.toXML(teamBlog));

}

}

运行结果:

<blogauthor="张三">

 <entry>

   <title>文章1</title>

   <description>这是文章1</description>

 </entry>

 <entry>

   <title>文章2</title>

   <description>这是文章2</description>

 </entry>

</blog>

 

属性别名

回到前面,在没有使用类别名的情况下所输出的标签名,显示的是全限定类名:xstream.Blog,现在需要将其更换为:cn.itcast.Blog

publicstaticvoid main(String[] args) {

Blog teamBlog =new Blog(new Author("张三"));

teamBlog.add(new Entry("文章1","这是文章1"));

teamBlog.add(new Entry("文章2","这是文章2"));

 

XStream xstream = new XStream();

xstream.aliasPackage("cn.itcast","xstream");

System.out.println(xstream.toXML(teamBlog));

}

结果:

<cn.itcast.Blog>

 <writer>

   <name>张三</name>

 </writer>

 <entries>

   <cn.itcast.Entry>

     <title>文章1</title>

     <description>这是文章1</description>

   </cn.itcast.Entry>

   <cn.itcast.Entry>

     <title>文章2</title>

     <description>这是文章2</description>

   </cn.itcast.Entry>

 </entries>

</cn.itcast.Blog>

posted on 2014-01-26 16:08  山庄听泉  阅读(402)  评论(0编辑  收藏  举报

导航