Dom4j生成XML文件时,使用utf-8出现中文乱码,可使用gbk编码解决,代码如下:
Document document = DocumentHelper.createDocument();
//根节点
Map inMap = new HashMap();
//xsl样式
inMap.put("type", "text/xsl");
inMap.put("href", "buban.xsl");
document.addProcessingInstruction("xml-stylesheet", inMap);
Element rootElement = document.addElement("info");
Element companyEle = rootElement.addElement("company");
companyEle.addText("某某公司");
//生成文件
OutputFormat outFormat = new OutputFormat();
outFormat.setEncoding("gbk");
XMLWriter writer = new XMLWriter(new FileWriter(new File("D:\test.xml"),outFormat);
writer.write(document);
writer.close();
阅读(2650) | 评论(0) | 转发(0) |