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

  • 相关阅读:
    IE6背景图片不显示,解决方法
    双语网站资源文件
    jQuery插件—获取URL参数
    如何在ashx页面获取Session值
    网站页面(aspx.cs)读取资源文件(*.resx)对应键值的value值
    .NET cache的使用
    Cache
    ASP.NET通过Global.asax和Timer定时器 定时调用WebService 运行后台代码
    ASP.NET 用户控件自定义属性、方法、事件
    jQuery获取Select选择的Text和Value
  • 原文地址:https://www.cnblogs.com/likehua/p/2029401.html
Copyright © 2011-2022 走看看