Xstream的使用

原文:http://xstream.codehaus.org/alias-tutorial.html

Xstream能很方便的实现XML<->JAVAOBJ,本文记录一下基本操作:

1.根节点
public class Blog {
	
	private Author Author;//Author 是BLOG的一级子节点
	
	/*entryList是BLOG的一级子节点,entry是entryList的一级子节点,entryList下有多个entry节点*/
    private List<Entry> entryList = new ArrayList<Entry>();

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

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

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

}
2.属性和子节点

public class Entry {
	
	private String title, description;
	
    public Entry(String title, String description) {
            this.title = title;
            this.description = description;
    }

}

public class Author {// Blog节点的一个属性
	private String name;// name是Author的子节点
	
    public Author(String name) {
            this.name = name;
    }
    public String getName() {
            return name;
    }

}
添加属性关键类:

public class AuthorConverter implements SingleValueConverter {

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

	@Override
	public Object fromString(String arg0) {
		return new Author(arg0);
	}

	@Override
	public String toString(Object arg0) {
		return ((Author) arg0).getName();
	}

}

public class XstreamDemo {

	public static String objToXml() {
		Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
		teamBlog.add(new Entry("11", "entry 实体一"));
		teamBlog.add(new Entry("22", "entry 试题二"));

		XStream xstream = new XStream();
		xstream.alias("blog", Blog.class);
		xstream.alias("entry", Entry.class);
		// 把标签Author改成writer
		// xstream.aliasField("writer", Blog.class, "Author");

		// 删除标签对entryList
		// xstream.addImplicitCollection(Blog.class, "entryList");

		// 给blog标签添加一个名为Author的属性
		xstream.useAttributeFor(Blog.class, "Author");
		xstream.registerConverter(new AuthorConverter());

		String xmlStr = xstream.toXML(teamBlog);
		System.out.println("OBJ——————>>>>>>>>XML \n" + xmlStr + "\n");
		return xmlStr;
	}

	public static void xmlToObj(String xmlStr) {
		XStream xstream = new XStream();

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

		Blog blog = (Blog) xstream.fromXML(xmlStr);
		System.out.println("XML----->OBJ");
		System.out.println("aa-->" + blog.entryList.size());
		for (int i = 0; i < blog.entryList.size(); i++) {
			System.out.println(blog.entryList.get(i));
		}
	}

}




3.测试:
public class Test {

	public static void main(String[] args) {
		String xmlCon = XstreamDemo.objToXml();
		XstreamDemo.xmlToObj(xmlCon);

	}

}

4.结果:

OBJ——————>>>>>>>>XML 
<blog Author="Guilherme Silveira">
  <entryList>
    <entry>
      <title>11</title>
      <description>entry 实体一</description>
    </entry>
    <entry>
      <title>22</title>
      <description>entry 试题二</description>
    </entry>
  </entryList>
</blog>


XML----->OBJ
Entry [title=11, description=entry 实体一]
Entry [title=22, description=entry 试题二]

5.相关jar包





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值