1. 程式人生 > >dom4j 輸出UTF-8 XML時中文亂碼

dom4j 輸出UTF-8 XML時中文亂碼

文件 put cep -s out throws 輸出 pat xml文件

使用DOM4J的XMLWriter輸出UTF-8編碼的XML文件時,出現亂碼

    public static void writToXml(Document document) throws IOException
    {
        OutputFormat format=OutputFormat.createPrettyPrint();
        XMLWriter writer=new XMLWriter(new FileOutputStream(fillpath),format);
        writer.write(document);//寫入文件
        
        format
=OutputFormat.createPrettyPrint(); writer=new XMLWriter(System.out,format);//輸出到屏幕 writer.write(document); }

第二段代碼在輸出屏幕的時候,輸出中文是亂碼的。

修改如下後即輸出中文了:

    public static void writToXml(Document document) throws IOException
    {
        OutputFormat format=OutputFormat.createPrettyPrint();
        XMLWriter writer
=new XMLWriter(new FileOutputStream(fillpath),format); writer.write(document); format=OutputFormat.createPrettyPrint(); format.setEncoding("gb2312"); writer=new XMLWriter(System.out,format); writer.write(document); }

dom4j 輸出UTF-8 XML時中文亂碼