2011-01-27 95 views
2

我已经开发出了下面的类来检索XML文档的节点列表:获取节点列表从XML文档

public class XMLDownloader { 
    public static NodeList getNodeList(){ 
     String url = "http://localhost/xml/example.xml"; 
     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpGet method = new HttpGet(url); 
     HttpResponse res = null; 
     NodeList result = null; 

     try { 
      res = client.execute(method); 
     } catch (ClientProtocolException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 

     try{ 
      InputStream is = res.getEntity().getContent(); 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse(is); 
      doc.getDocumentElement().normalize(); 
      result = doc.getElementsByTagName("client");       
      is.close(); 

     }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
     }  
     return result;    
    } 
} 

但是这个方法返回null。有任何想法吗?

的xml文件:

<status> 
<client type="s" name="test1" protocol="1000"> 
     </client> 
<client type="r" name="test2" protocol="2000"> 
     </client> 
<client type="r" name="test3" protocol="3000"> 
     </client> 
<client type="h" name="test4" protocol="4000"> 
     </client> 
<client type="c" name="test5" protocol="5000"> 
     </client> 
</status> 
+0

你可以发布你的example.xml文件吗?或者指出哪一部分为你返回null – kgutteridge 2011-01-27 17:36:15

回答

0

张贴在讨论的代码实际工作对我来说没有任何错误。我得到一个包含预期5个元素的NodeList。我在运行2.2的模拟器上试了一下。