2015-07-11 24 views
0

我的外部XML已經有無法分析UTF-8 XML

<?xml version="1.0" encoding="UTF-8"?> 

然而,當我嘗試解析它在我的應用程序,它不讀取Unicode的了!

這是我所做的,仍然沒有運氣。

private class MyDownloadTask extends AsyncTask<Void,Void,Void> 
{ 
    String URL = context.getResources().getString(R.string.XML_database_url); 
    String KEY_ITEM = "item"; // parent node 
    String KEY_NAME = "name"; 
    String KEY_COST = "location"; 
    String KEY_DESC = "url"; 
    ArrayList<RadioListElement> radioArray; 

    protected void onPreExecute(final ArrayList<String> userRadios) { 
     super.onPreExecute(); 
     radioArray = new ArrayList<RadioListElement>(); 
     MainActivity.getDataManager().loadStoredRadioStations(radioArray, userRadios); 
    } 

    protected Void doInBackground(Void... params) { 
     String xml = getXmlFromUrl(URL); 
     Document doc = getDomElement(xml); 

     NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
     for (int i = 0; i < nl.getLength(); i++) { 
      Element e = (Element) nl.item(i); 
      String name = getValue(e, KEY_NAME); 
      String cost = getValue(e, KEY_COST); 
      String description = getValue(e, KEY_DESC); 
      radioArray.add(new RadioListElement(context, name, cost, description)); 
     } 
     return null; 
} 

public Document getDomElement(String xml){ 
     Document doc = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 

      DocumentBuilder db = dbf.newDocumentBuilder(); 

      InputSource is = new InputSource(is,"UTF-8"); 
      is.setCharacterStream(new StringReader(xml)); 

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

我把UTF-8這裏

   InputSource is = new InputSource(is,"UTF-8"); 

我在做什麼錯?我怎樣才能使這項工作,以便它顯示Unicode對我來說很好?

+0

你能告訴我們String xml來自哪裏嗎?它可能會出錯,因爲你需要用String來指定編碼。 – cvesters

+0

具有XML文件的Unicode字節順序標記集? –

+0

@cvesters xml沒有錯,因爲它是相當標準的把我提到的標題。 – thevoipman

回答

0

我將utf-8添加到從url獲取xml的代碼中。應該是這樣的:

xml = EntityUtils.toString(httpEntity,"utf-8");

public String getXmlFromUrl(String url) { 
    String xml = null; 
    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity,"utf-8"); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return xml; 
} 
1

不要試圖XML轉換爲字符串,你的自我,並嘗試喂字符串DOM解析器。 xml解析器是智能的,可以解釋它們自身的編碼。

我建議改變getXmlFromUrl(String url)返回從httpEntity如下InputStream

return httpEntity.getContent() 

給下面這個InputStream到DOM解析器:沒有編碼在is

設置

InputSource is = new InputSource(inputStream); 

現在解析此is並驗證它解析unicode如預期