Dom4j生成XML文件中文乱码问题
时间:2010-07-14 来源:qdl2010
Dom4j生成XML文件时,使用utf-8出现中文乱码,可使用gbk编码解决,代码如下:
Document document = DocumentHelper.createDocument();
//根节点
Map<String, String> inMap = new HashMap<String, String>(); //xsl样式
inMap.put("type", "text/xsl"); inMap.put("href", "buban.xsl"); document.addProcessingInstruction("xml-stylesheet", inMap);
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();
Map<String, String> inMap = new HashMap<String, String>(); //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();
相关阅读 更多 +