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();
        };
        
       };
      }; 

  • 相关阅读:
    Ceph实验室:第六课:Ceph运维之横向扩展
    Ceph实验室:第五课:Ceph运维之换盘
    百度2014软件开发工程师笔试题详解 (转)
    阿里巴巴2014笔试题详解(9月22北京)(转)
    阿里巴巴2014秋季校园招聘-软件研发工程师笔试题详解(转)
    腾讯的2014年校招的软开笔试题(转)
    typedef与define区别
    java流总结(转)
    java 流 复制,重命名,删除目录
    java 流 读
  • 原文地址:https://www.cnblogs.com/likehua/p/2029401.html
Copyright © 2011-2022 走看看