Xstream之常用方式与常用注解

参考资料
1 xStream框架完美实现Java对象和xml文档JSON、XML相互转换
http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
2 xStream完美转换XML、JSON
http://archive.cnblogs.com/a/2025197/
官网:http://xstream.codehaus.org/download.html
相关jar包可在:
XStream之初识
http://liuzidong.iteye.com/blog/1059453下载
示例代码
Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
eamBlog.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));

打印结果为:
 <xtream.Blog>
  <writer>
    <name>Guilherme Silveira</name>
  </writer>
  <entries>
    <xtream.Entry>
      <title>first</title>
      <description>My first blog entry.</description>
    </xtream.Entry>
    <xtream.Entry>
      <title>tutorial</title>
      <description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description>
    </xtream.Entry>
  </entries>
</xtream.Blog>

以上节点就包含了包名,如包名很长,就很难看了,怎样才能重新命名这个节点呀!
以下的1,2设置效果一样
1 //xstream.aliasPackage("", "xtream");
2 xstream.alias("blog", Blog.class);
3 xstream.alias("entry", Entry.class);
 
通过这样设置就完成了节点名称的设置.如下:
<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>

修改子节点属性名称
将writer属性名称修改为:autor
xstream.aliasField("author", Blog.class, "writer");

输出如下:
<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>

删除集合节点名称的设置
xstream.addImplicitCollection(Blog.class, "entries");

输出如下:
 <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>

将Blog类的元素设置为:它的节点属性
//使用这个属性名作为节点上的元素
xstream.useAttributeFor(Blog.class, "writer");

//重新命名
xstream.aliasField("author", Blog.class, "writer");

//注册转换器
xstream.registerConverter(new AuthorConverter());

转换器:
import com.thoughtworks.xstream.converters.SingleValueConverter;
class AuthorConverter implements SingleValueConverter {
	//显示节点属性值
	public String toString(Object obj) {
		return ((Author) obj).getName();
	}

	public Object fromString(String name) {
		return new Author(name);
	}

	public boolean canConvert(Class type) {
		return type.equals(Author.class);
	}
}

显示结果:
<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>

注解的使用方式,使用之前必须加上注解类才有作用:
XStream xstream = new XStream();
xstream.processAnnotations(Blog.class);
xstream.processAnnotations(Entry.class);

@XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("blog", Blog.class);
@XStreamAlias("blog")
public class Blog {	
	
	@XStreamAlias("author")
        private Author writer;

	.....
}

当然Entry类也要设置同样的类属性:@XStreamAlias("entity")
2 @XStreamImplicit去集合节点名:相当于 xstream.addImplicitCollection(Blog.class, "entries");
3 @XStreamConverter(AuthorConverter.class),参见以上的转换器类相当于:
xstream.useAttributeFor(Blog.class, "writer");
//重新命名
xstream.aliasField("author", Blog.class, "writer");
//注册转换器
xstream.registerConverter(new AuthorConverter());

要使用这个注解AuthorConverter必须符合二个条件
a 必须要有个默认的无参构造函数
public AuthorConverter() {
		
}

b 类声明必须为:public
public class AuthorConverter implements SingleValueConverter {
 ...
}

不用注解这二点都可以不强制要求!
完整代码如下:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("blog")
public class Blog {

	@XStreamAsAttribute
	@XStreamAlias("author")
	@XStreamConverter(AuthorConverter.class)
	private Author writer;

	@XStreamImplicit
	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;
	}

	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.processAnnotations(Blog.class);
		xstream.processAnnotations(Entry.class);
		// 重新命名节点名
		// xstream.aliasPackage("", "xtream");
		/*
		 * 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.aliasField("author", Blog.class, "writer"); //
		 * xstream.addImplicitCollection(Blog.class, "entries");
		 * //使用这个属性名作为节点上的元素 xstream.useAttributeFor(Blog.class, "writer");
		 * //重新命名 xstream.aliasField("author", Blog.class, "writer"); //注册转换器
		 * xstream.registerConverter(new AuthorConverter());
		 */
		System.out.println(xstream.toXML(teamBlog));
	}

}

你可能感兴趣的:(java,json,xml,Blog,ITeye)