2012-09-18 32 views
0

我試圖從UTF-8得到DOM元素編碼的XML解析文件包含阿拉伯字符。 下面的方法使用解析的xml字符串,並且應該返回Document。從XML解析形成DOM元素的錯誤字符串

這裏是對XML的鏈接:

http://212.12.165.44:7201/UniNews121.xml

public Document getDomElement(String xml){ 

    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 

    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 
     InputSource is = new InputSource(); 
     StringReader xmlstring=new StringReader(xml); 
     is.setCharacterStream(xmlstring); 
     is.setEncoding("UTF-8"); 
        //APP CRASHES HERE 
     doc = db.parse(is); 

    } catch (ParserConfigurationException e) { 
     Log.e("Error: ", e.getMessage()); 
     return null; 
    } catch (SAXException e) { 
     Log.e("Error: ", e.getMessage()); 
     return null; 
    } catch (IOException e) { 
     Log.e("Error: ", e.getMessage()); 
     return null; 
    } 
    // return DOM 
    return doc; 
} 

錯誤:

09-18 13:36:20.031: E/Error:(3846): Unexpected token (position:TEXT xml version="[email protected]:1 in [email protected]) 

我很感激你的幫助,但請具體說明在你的答案

+1

請添加完整的堆棧跟蹤(和一個小的xml,再現問題) – njzk2

+0

親切檢查更新@ njzk2 –

+0

你可以發佈完整的堆棧跟蹤嗎?根據我所見, – njzk2

回答

0

它發生了很多次,我應該仔細檢查你正在打開的文件的編碼。我建議你用手工設置編碼的文件的本地副本來測試。

+0

如果我沒有包含阿拉伯文字符 –

+0

,即使使用中文字母等其他字母,該代碼也可以正常工作?你可以進行測試嗎? – Gabber

+0

順便說一句,我忘了提及,我創建了XML文件,所以在保存時我一定要保存在UTF-8中,並且我將它發佈到服務器上,所以我不認爲它的文件問題 –