1
我不知道爲什麼,但是Log.i(TAG,「found:」+ found);在我的XML中爲「found」元素返回null,表示我獲得了一個流。我已經調試過,看來我的XML的根元素被讀取,但是這個根元素的元素不是。 urlString也是正確的。文檔被解析,沒有任何錯誤和異常。但在這一行在Android中解析XML流
NodeList nl = docEle.getElementsByTagName("found");
found = nl.item(0).getNodeValue();
發現爲空。這是一個方法的全碼:
protected Void doInBackground(String... urls) {
String urlString = urls[0];
URL documentUrl = null;
InputStream stream = null;
URLConnection conn = null;
DocumentBuilder builder = null;
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
documentUrl = new URL(urlString);
conn = documentUrl.openConnection();
stream = conn.getInputStream();
document = builder.parse(stream);
}
catch (IOException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
catch (SAXException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
catch (ParserConfigurationException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
finally
{
if (stream != null)
{
try {
stream.close();
} catch (IOException e) {
Error = e.getMessage();
Log.i(TAG, "Exception!", e);
}
}
}
/*NodeList nodes = document.getElementsByTagName("found");
for (int i = 0; i < nodes.getLength(); i++) {
found = nodes.item(i).getNodeValue();
//System.out.println(found);
Log.i(TAG, "found: "+found);
}*/
//get the root element
Element docEle = document.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("found");
found = nl.item(0).getNodeValue();
Log.i(TAG, "found: "+found);
return null;
}
這是我urlString與XML:http://basa.med-info.ru/xse/index.php?query=%E3%F0%E8%EF%EF&nres=20
發現必須等於20
了,謝謝。是問題 – wzbozon 2012-02-20 11:42:48