Dom4j生成XML包含中文的解决方法

try {
    FileWriter fileWriter = new FileWriter(new File(storePath
     + xmlFileName));
   XMLWriter output = new XMLWriter(fileWriter);
   output.write(document);
   //关闭
   output.close();
  } catch (Exception e) {
   log.info("(CMS-XML/Copyright)Excep:" + e.getMessage());
  }

在采用DOM4J生成XML文件时,在包含中文,有空可能出现字符集的问题,只要增加一下字符集的设置即可

如下面的代码段

try {
   //设置XML的encoding字符集
   OutputFormat format = OutputFormat.createPrettyPrint();
   format.setEncoding("GBK");
   FileWriter fileWriter = new FileWriter(new File(storePath
     + xmlFileName));
   XMLWriter output = new XMLWriter(fileWriter, format);
   output.write(document);
   //关闭
   output.close();
  } catch (Exception e) {
   log.info("(CMS-XML/Copyright)Excep:" + e.getMessage());
  }  

当然也有其它的解决方法,可能会更好。但这也算得是一种。

你可能感兴趣的:(cms,xml)