我有一个XML文件,其中包含一些从URL中检索到的阿拉伯字符,所以我必须使用UTF-8对其进行编码,以便它可以处理这些字符。解析UTF-8编码的XML文件
XML文件:
<Entry>
<lstItems>
<item>
<id>1</id>
<title>News Test 1</title>
<subtitle>16/7/2012</subtitle>
<img>joelle.mobi-mind.com/imgs/news1.jpg</img>
</item>
<item>
<id>2</id>
<title>كريم</title>
<subtitle>16/7/2012</subtitle>
<img>joelle.mobi-mind.com/imgs/news2.jpg</img>
</item>
<item>
<id>3</id>
<title>News Test 333</title>
<subtitle>16/7/2012</subtitle>
<img>joelle.mobi-mind.com/imgs/news3.jpg</img>
</item>
<item>
<id>4</id>
<title>ربيع</title>
<subtitle>16/7/2012</subtitle>
<img>joelle.mobi-mind.com/imgs/cont20.jpg</img>
</item>
<item>
<id>5</id>
<title>News Test 55555</title>
<subtitle>16/7/2012</subtitle>
<img>joelle.mobi-mind.com/imgs/cont21.jpg</img>
</item>
<item>
<id>6</id>
<title>News Test 666666</title>
<subtitle>16/7/2012</subtitle>
<img>joelle.mobi-mind.com/imgs/cont22.jpg</img>
</item>
</lstItems>
</Entry>
我解析从URL作为字符串检索XML,如下图所示:
public String getXmlFromUrl(String url) {
try {
return new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
//String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = new String(EntityUtils.toString(httpEntity).getBytes(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return xml;
}
}.execute(url).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
}
现在返回的字符串传递给此方法来获取供日后使用的文件如下图所示:
此消息ocured错误:
09-18 07:51:40.441: E/Error:(1210): Unexpected token (position:TEXT @1:4 in [email protected])
因此,代码崩溃,我有以下错误
09-18 07:51:40.451: E/AndroidRuntime(1210): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.university1/com.example.university1.MainActivity}: java.lang.NullPointerException
上面显示请注意,该代码工作正常使用ISO编码。
Upvoted仅仅因为有两个没有评论的评论(没有任何明显的理由,这个问题是合理的)。 – bortzmeyer
我也这么做了。 – Neta