JDom输出UTF8的XML完美解决

现象描述:JDom输出Xml文件,当使用字符编码GBK时正常,而输出UTF-8时乱码。

    完美的解决方法从辟谣开始:
    1)JDOM是否生成UTF-8的文件与Format是否设置无关,只有输出其他字符编码才需要设置,见下面的注释。
    2)JDOM输出UTF-8文件乱码的根本原因并非在JDOMAPI,而是在JDK。

    具体描述:
        JDOM的输出类XMLOutputter有两个output接口,除了都具有一个Document参数外,分别接受Writer和OutputStream参数。
        这给我们一个错觉,两个接口可以任意使用。
        首先我们用output(doc,System.out)来做测试,此时得到乱码,
          然后我们改为output(doc,new PrintWriter(System.out))来测试,输出不是乱码,
          也就是说在控制台的时候一定要用一个Writer接口包装一下。
        然后我们用output(doc,new FileWriter(path))来做测试,结果却得到乱码,
          然后我们改为output(doc,new FileOutputStream(path))来测试,输出不是乱码,
          也就是说在输出文件的时候一定要用一个OutputStream接口包装一下。
        疯了吧?呵呵,很搞笑是吧。经过到JDOM的源码中调试,发现没有任何问题,问题出在了JDK里面。

    JDK内的对应接口处理:
    1)PrintWriter类有参数为OutputStream的构造方法,因此可以从System.out包装到PrintWriter
    2)FileWriter类没有参数为OutputStream的构造方法,因此不能从FileOutputStream包装到FileWriter
    3)如果PrintWriter类用了参数为Writer的构造方法(Writer实现为FileWriter),最后输出也是乱码
    4)如果用一个FileOutputStream来包装一个控制台输出,也是乱码
    因此,对于JDK内的各种输出体系,各种InputStream、OutputStream、reader和writer要充分认识,否则极容易出现一些意想不到的问题。

    测试的JDOM版本:1.0、1.1
    测试代码:

  1. import java.io.File;  
  2.     import java.io.FileOutputStream;  
  3.     import java.io.FileWriter;  
  4.     import java.io.PrintWriter;  
  5.     import java.util.HashMap;  
  6.     import org.jdom.Document;  
  7.     import org.jdom.Element;  
  8.     import org.jdom.output.Format;  
  9.     import org.jdom.output.XMLOutputter;  
  10.     public class BuildXML {  
  11.      public static void main(String[] args) throws Exception{  
  12.       File xmlfile=new File("C://EditTemp//xml//abc.xml");  
  13.       //中文问题 //GBK 是没有问题的,但UTF-8就是有问题的  
  14.       //原因:  
  15.       //1)对于磁盘文件,必须使用输出流 FileOutputStream  
  16.       //     FileWriter out=new FileWriter(xmlfile);会导致乱码  
  17.       //2)对于控制台输出,则必须使用PrintWriter,如果直接使用System.out也会出现乱码  
  18.       //     PrintWriter out=new PrintWriter(System.out);  
  19.       FileOutputStream out=new FileOutputStream(xmlfile);  
  20.          Element eroot=new Element("root");  
  21.          eroot.addContent((new Element("code")).addContent("代码"));  
  22.          eroot.addContent((new Element("ds")).addContent("数据源"));  
  23.          eroot.addContent((new Element("sql")).addContent("检索sql"));  
  24.          eroot.addContent((new Element("order")).addContent("排序"));  
  25.          Document doc=new Document(eroot);  
  26.          XMLOutputter outputter = new XMLOutputter();  
  27.          //如果不设置format,仅仅是没有缩进,xml还是utf-8的,因此format不是必要的  
  28.          Format f = Format.getPrettyFormat();  
  29.          //f.setEncoding("UTF-8");//default=UTF-8  
  30.          outputter.setFormat(f);  
  31.          outputter.output(doc, out);  
  32.          out.close();  
  33.      }  
  34.     }  

    另附一方法,将JDOM的Document对象,根据指定的编码输出字符串:

  1. /** 
  2.      * 这个方法将JDom Document对象根据指定的编码转换字符串返回。 
  3.      * @param xmlDoc 将要被转换的JDom对象 
  4.      * @param encoding 输出字符串使用的编码 
  5.      * @return String Document经处理生成的字符串 
  6.      * @throws IOException  
  7.      */  
  8.     public static String toXML(Document xmlDoc, String encoding) throws IOException  
  9.     {  
  10.         ByteArrayOutputStream byteRep = new ByteArrayOutputStream();  
  11.         PrintWriter out=new PrintWriter(byteRep);  
  12.         Format format = Format.getPrettyFormat();  
  13.         format.setEncoding(encoding);  
  14.         XMLOutputter docWriter = new XMLOutputter(format);  
  15.         try {  
  16.             docWriter.output(xmlDoc, out);  
  17.         } catch (Exception e) {  
  18.         }  
  19.         return byteRep.toString();  
  20.     }  
原文地址:https://www.cnblogs.com/duanxz/p/2615167.html