问题
I used a tutorial about listview parsing xm from internet and using LasyAdapter shows the items in listview. When I add persian characters in xml (into one of childnodes) the result is some boxes in listview (after showing the text in listview). The format of xml is UTF-8, too. I used typeface too (but didn't work). Besides when I type Pwesian into the application it shows alright but it can't show Persiann content parsed from xml. Thanks in advance. I updated the post with original XMLparser (which was the problem).
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
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 doc;
}
回答1:
I could find the answer. Thanks from @Serdak. He made me think of reader. In getXMLFromUrl part I should have changed:xml = EntityUtils.toString(httpEntity);
to xml = EntityUtils.toString(httpEntity, "UTF-8");
and in getDomElement part I should change:InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml));
to InputSource is = new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Please note that "url" and "xml" are both Strings which are called by main activity and are used in XMLparser.jave (which contains both getXmlFromUrl() and getDomElement() ).
These commands are in main activity:XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL)
;Document doc = parser.getDomElement(xml);
Best regards.
回答2:
The reader that you use to read the Xml values is probably not set to UTF-8 and that's why causing encoding problem.
Try using your reader with an encoding option like this.
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
回答3:
The StringReader Object would make an exception when the XML string contains Unicode characters such as Persian letters. So please pass the InputStream object to the Document object straightly
Thank you
来源:https://stackoverflow.com/questions/12660235/listview-parsing-persian-greek-arabic-or-utf-8-xml