我的外部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對我來說很好?
你能告訴我們String xml來自哪裏嗎?它可能會出錯,因爲你需要用String來指定編碼。 – cvesters
具有XML文件的Unicode字節順序標記集? –
@cvesters xml沒有錯,因爲它是相當標準的把我提到的標題。 – thevoipman