使用 XStream 读写 XML 文件
导入 gradle 依赖:
compile 'com.thoughtworks.xstream:xstream:1.4.8'
compile 'dom4j:dom4j:1.6.1'
// 写入 xml 文件
// http://wenku.baidu.com/link?url=OsNoP52Gl_MGUVmoAks9aS4cHz9_8HfdKwz3yHanK9B1U2Q8p-OIfU0St32BQGjd4XuP5Mm0t7nU82ULz9Yme_3pg0EcY8SRbHZrQ61ugS3
XStream xStream = new XStream(new Dom4JDriver()); // new StaxDriver()
Corpus corpus = new Corpus();
corpus.setCorpusWordList(corpusBeanList);
// 参考资料:http://blog.sina.com.cn/s/blog_77a2f9ae01017ucf.html
// 不显示集合元素标签
// addImplicitCollection(Class ownerType, String fieldName),去掉集合类型生成xml的父节点。
xStream.addImplicitCollection(Corpus.class, "corpusWordList");
xStream.alias("corpus", Corpus.class);
xStream.alias("CorpusWord",CorpusWord.class);
PrintWriter pw = new PrintWriter("corpus.xml","utf-8");
xStream.toXML(corpus,pw);