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