XStream的基本用法

最近用到XStream,总结下基本的使用,主要如下:

1.使用类的别名来改变标签的名字

2.使用字段的别名来改变标签的名字

3.使用包的别名来改变标签的名字

4.如果字段类型是由一个SingleValueConverter处理,可写为属性


贴代码:Blog.java

public class Blog {

	private Author writer;

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

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

	public Author getWriter() {
		return writer;
	}

	public void setWriter(Author writer) {
		this.writer = writer;
	}

	public List<Entry> getEntries() {
		return entries;
	}

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

	public void add(Entry entry) {
		entries.add(entry);
	}

	public List<Entry> getContent() {
		return entries;
	}

}


Author.java

public class Author {
		
	private String name;

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

	public String getName() {return name;}

	public void setName(String name) {this.name = name;}

}

Entry.java

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

}

XStream.java

public class XStreamTest {
	
	public static void 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( new DomDriver());
		
		System.out.println(xstream.toXML(teamBlog));
	}
	
}

运行结果:

<myXStream.Blog>
  <writer>
    <name>张三</name>
  </writer>
  <entries>
    <myXStream.Entry>
      <title>文章1</title>
      <description>这是文章1</description>
    </myXStream.Entry>
    <myXStream.Entry>
      <title>文章2</title>
      <description>这是文章2</description>
    </myXStream.Entry>
  </entries>
</myXStream.Blog>


修改一下XStream.java

public class XStreamTest {
	
	public static void 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( new DomDriver());
		//用别名“blog”替代Blog.class
		xstream.alias("blog", Blog.class);
		
		xstream.alias("Entry", Entry.class);
		//用别名“author”替代Blog.class的“write”属性
		xstream.aliasField("author", Blog.class, "writer");
		//让“entries”不显示
		xstream.addImplicitCollection(Blog.class, "entries");
		
		System.out.println(xstream.toXML(teamBlog));
	}
	
}
运行结果:

<blog>
  <author>
    <name>张三</name>
  </author>
  <Entry>
    <title>文章1</title>
    <description>这是文章1</description>
  </Entry>
  <Entry>
    <title>文章2</title>
    <description>这是文章2</description>
  </Entry>
</blog>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值