zoukankan      html  css  js  c++  java
  • DOM4J 生成中文XML出现乱码

    1 使用FileWriter写中文xml出现乱码   很怪异。

    XMLWriter writer=null;
      try{
       Document doc=DocumentHelper.parseText(result);
       OutputFormat format=OutputFormat.createPrettyPrint();
       format.setEncoding("UTF-8");
             //FileOutputStream fos=new FileOutputStream(new File("C:\\GeoSearchResponse.xml"));
       writer=new XMLWriter(new FileWriter(new File("C:\\GeoSearchResponse.xml")),format);
             //writer=new XMLWriter(fos,format);
       writer.write(doc);
       System.out.println("xml:"+doc.asXML());
      }catch(DocumentException w){
               w.printStackTrace();  
      }catch(IOException i){
       i.printStackTrace();
      }finally{
       if(writer!=null){
        try{
         writer.close();
        }catch(IOException e){
         e.printStackTrace();
        };
        
       };
      }; 

    2  经过反复测试和实验   改成FileOutputStream来写  结果良好  

    XMLWriter writer=null;
      try{
       Document doc=DocumentHelper.parseText(result);
       OutputFormat format=OutputFormat.createPrettyPrint();
       format.setEncoding("UTF-8");
             FileOutputStream fos=new FileOutputStream(new File("C:\\GeoSearchResponse.xml"));
       //writer=new XMLWriter(new FileWriter(new File("C:\\GeoSearchResponse.xml")),format);
             writer=new XMLWriter(fos,format);
       writer.write(doc);
       System.out.println("xml:"+doc.asXML());
      }catch(DocumentException w){
               w.printStackTrace();  
      }catch(IOException i){
       i.printStackTrace();
      }finally{
       if(writer!=null){
        try{
         writer.close();
        }catch(IOException e){
         e.printStackTrace();
        };
        
       };
      }; 

  • 相关阅读:
    C#3.0之神奇的Lambda表达式和Lambda语句
    Expression Tree 学习笔记(一)
    C#对象序列化与反序列化
    Linux Shell编程入门
    ora-03113或者ora-12573 通信通道的文件结束出现异常错误:核心转储
    如何实现文档在线预览
    使用npoi导入Excel
    判断时间(时:分)是否在某个时间段内
    程序员开发时遇到的那些缩写和名词(记录)
    git
  • 原文地址:https://www.cnblogs.com/likehua/p/2029401.html
Copyright © 2011-2022 走看看