---
title: "使用xStream进行java object<-->xml之间的转换"
author: "moogle"
date: "2006-11-16T23:28:53Z"
tags: []
slug: "xstream-java-object-xml"
draft: false
lastmod: "2006-11-16T23:28:53Z"
id: 34661
url: "https://moogle.iteye.com/blog/34661"
---

官方网站:http://xstream.codehaus.org/

测试了一下，的确十分方便。

java 代码

01. public static void write() {
02. XStream sm = new XStream();
03. mytest t = new mytest();
04. t.setName("moogle");
05. t.setXb("男");
06. try {
07. FileOutputStream ops = new FileOutputStream(new File("C:\\\\111.xml"));
08. sm.toXML(t, ops);
09. ops.close();
10. } catch (Exception e) {
11. e.printStackTrace();
12. }
13. }
14. public static void read() {
15. XStream sm = new XStream(new DomDriver());
16. try {
17. FileInputStream ops = new FileInputStream(new File("C:\\\\111.xml"));
18. mytest t = (mytest)sm.fromXML(ops);
19. System.out.println(t.getName());
20. ops.close();
21. } catch (Exception e) {
22. e.printStackTrace();
23. }
24. }

生成的XML文件内容:

xml 代码

1. &lt;mytest&gt;
2. &lt;name&gt;asd&lt;/name&gt;
3. &lt;xb&gt;男&lt;/xb&gt;
4. &lt;/mytest&gt;

问题:  
1.生成的xml文档没有

2.如果生成的文档中含有中文，比如上文代码中setXb("男")  
在读取的时候会报  
**\[Fatal Error] :4:7: Invalid byte 2 of 2-byte UTF-8 sequence.**

**请指教。**