UTF-8修改(写xml文件乱码),报Invalid byte 2 of 2-byte UTF-8 sequence错误。写gb2312和gbk没事,写xml是网上的例子,用了FileWirter。然后找到
博客:http://blog.csdn.net/redez/archive/2005/11/11/527897.aspx
说到用FileOutPutStream
看例子(是个测试类,只给出这个修改方法,测试类请看——dom4j 读 xml:http://www.blogjava.net/chenlb/archive/2007/09/05/143036.html):
public void testModXml() {
List list = document.selectNodes("/company/tel" );
Random rm = new Random();
for(Element tel : (List<Element>) list) {
logger.info("tel: "+tel.getTextTrim());
tel.setText("020-12345678-"+rm.nextInt(100));
}
Element root = document.getRootElement();
for(int i=0; i<2; i++) {
Element tel = root.addElement("tel");
tel.setText("020-12345678-"+rm.nextInt(100));
}
XMLWriter writer;
try {
OutputFormat format = OutputFormat.createPrettyPrint();
//format.setEncoding("UTF-8");
FileOutputStream fos = new FileOutputStream(xmlFile);
//writer = new XMLWriter(new FileWriter(xmlFile), format);
writer = new XMLWriter(fos, format);
writer.write(document);
writer.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
logger.error("修改xml文件失败!");
e.printStackTrace();
}
}
posted on 2007-09-06 00:10
流浪汗 阅读(6174)
评论(1) 编辑 收藏 所属分类:
OpenSource