2011-01-27 7 views
2

から私は、XML文書のノード・リストを取得するには、次のクラス開発している:取得のNodeList 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

+0

応答エンティティの内容に正しいものが含まれていることを確認してください。 Eclipseデバッガを使用して、各ポイントのオブジェクトをステップスルーして検査して、ドキュメントにあなたが思うものが含まれていることを確認します。 –

+0

コードはエラーを記録しましたか(catch句の)スタックトレースを記録しましたか?もしそうなら、彼らは何でしたか? (ここではnormalize()を呼び出す必要はありません。parseは常に正規化されたDocumentを返します。コード内のdocツリーを変更する場合はnormalize()が便利です。 –

答えて

0

質問に投稿されたコードは、実際にエラーが発生することなく私のために動作します。私は、期待される5要素を含むNodeListを取得します。 2.2を走らせているエミュレータで試してみました。

関連する問題