XML 中文問題

起因:

     在使用SAXParser解析XML文檔, 當XML文檔中包含中文時出錯.

解決方案:

       在對XML進行解析之前, 將String形式的XML文檔, 都轉換成UTF-8格式. 轉換代碼如下;

 public static String toUTF_8(final String str){
  if(str==null)
   return str;
   
  String retVal = str;
  try{
   retVal = new String(str.getBytes("ISO8859_1"), "UTF-8");
  } catch(Exception e){
   //log...
  }
  
  return retVal;
 }

續:

      今天發現問題的根本原因並不如下所述, 而是String轉換成產生亂碼, 使用的是StringBufferInputStream, 注意:這個類來能進行正確的編碼轉換, 使用ByteArrayInputStream, 代碼如下:

   InputStream stream = null;
   try{
    stream = new ByteArrayInputStream(xml.getBytes("utf-8"));
   }catch (Exception e){
    
   }

相關鏈接:

http://www.vr-breeze.org/forum/archiver/?tid-67.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章