java解析Xml 保存格式设置

来源:互联网 发布:水利设计软件 编辑:程序博客网 时间:2024/05/17 04:42

    // 声明写XML的对象
    XMLWriter writer = null;            
    SAXReader reader = new SAXReader();           
    OutputFormat format =  new OutputFormat();
    // 设置XML文件的编码格式         
    format.setEncoding("UTF-8");
   
    String filePath = ServletActionContext.getServletContext().getRealPath("\\") + "index\\mapData.xml" ;
    File file = new File(filePath); 

   ……

   ……
     

     writer = new XMLWriter(new FileWriter(filePath), format);    //xml保存时并没有保存为UTF-8编码格式
     writer.write(document);
     writer.close();

 

console:

     org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.

    ******************************************************************************************************************************************

    更改为  

    // 声明写XML的对象
    XMLWriter writer = null;            
    SAXReader reader = new SAXReader();            
        
    String filePath = ServletActionContext.getServletContext().getRealPath("\\") + "index\\mapData.xml" ;
    File file = new File(filePath);

   ……

   ……
     

     writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
     writer.write(document);
     writer.close();

   

 

 

   

   

 

    

 

 

 

 

0 0