2012-02-20 3 views
1

Я не знаю почему, но Log.i (TAG, "found:" + found); возвращает null для элемента «found» в моем XML, который я получаю в потоке. Я отлаживался, и кажется, что мой корневой элемент XML читается, но элементы этого корневого элемента не являются. urlString также верна. документ анализируется без каких-либо ошибок и исключений. Но в этих строкахАнализ XML-потока в Android

NodeList nl = docEle.getElementsByTagName("found"); 
     found = nl.item(0).getNodeValue(); 

обнаружено null. Это полный код метода:

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.

ответ

2

использования:

found = nl.item(0).getFirstChild().getNodeValue(); 
+0

спасибо, что была проблема – wzbozon