官方网站:http://xstream.codehaus.org/
测试了一下,的确十分方便。
java 代码
- public static void write() {
- XStream sm = new XStream();
- mytest t = new mytest();
- t.setName("moogle");
- t.setXb("男");
- try {
- FileOutputStream ops = new FileOutputStream(new File("C:\\111.xml"));
- sm.toXML(t, ops);
- ops.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void read() {
- XStream sm = new XStream(new DomDriver());
- try {
- FileInputStream ops = new FileInputStream(new File("C:\\111.xml"));
- mytest t = (mytest)sm.fromXML(ops);
- System.out.println(t.getName());
- ops.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
生成的XML文件内容:
xml 代码
- <mytest>
- <name>asd</name>
- <xb>男</xb>
- </mytest>
问题:
1.生成的xml文档没有
2.如果生成的文档中含有中文,比如上文代码中setXb("男")
在读取的时候会报
[Fatal Error] :4:7: Invalid byte 2 of 2-byte UTF-8 sequence.
请指教。