2011-01-27 6 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()를 호출 할 필요가 없습니다. 구문 분석은 항상 정규화 된 Document를 반환합니다.) normalize()는 코드에서 doc 트리를 수정하는 경우 유용합니다. –

답변

0

질문에 게시 코드 실제로 오류없이 나를 위해 작동합니다. 예상되는 5 요소를 포함하는 NodeList를 얻습니다. 나는 2.2를 실행하는 에뮬레이터에서 그것을 시도했다.