2015-09-20 23 views
0

我有一個XmlResourceParser實例,名爲xml。當我嘗試在節點上調用getText()時,如我的代碼所示,它返回null。這很奇怪,因爲我可以在同一個節點上調用getName()它返回適當的值,所以實例設置正確。這裏是我的代碼:XmlResourceParser.getText()返回null

XmlResourceParser xml = context.getResources().getXml(R.xml.thesaurus); 

    try { 
     //if (xml.getName().equals("word")) { 
      xml.next(); //to the first node within <word></word> 
      boolean notFound = true; 
      while (notFound) { 
       xml.next(); 
       if (xml.getName() != null && xml.getName().equalsIgnoreCase("synonyms")) { 
        String synonym = xml.getText(); 
        Log.v(TAG, String.valueOf(synonym)); 
        notFound = false; //found 
       } 
      } 
     } 
    } catch (XmlPullParserException xppe) { 
     xppe.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 

這裏是我的XML,即使沒有什麼錯:

<?xml version="1.0"?> 
<thesaurus> 
    <word name="let"> 
     <synonyms>allow</synonyms> 
    </word> 
</thesaurus> 

任何幫助,將不勝感激!謝謝!

回答

1

我發現我自己的答案here.我以前張貼的代碼@Libin這個答案。

int eventType = xmlResourceParser.getEventType(); 
    while (eventType != XmlPullParser.END_DOCUMENT) { 
     if (eventType == XmlPullParser.START_DOCUMENT) { 
      System.out.println("Start document"); 
     } else if (eventType == XmlPullParser.START_TAG) { 
      System.out.println("Start tag " + xmlResourceParser.getName()); 
     } else if (eventType == XmlPullParser.END_TAG) { 
      System.out.println("End tag " + xmlResourceParser.getName()); 
     } else if (eventType == XmlPullParser.TEXT) { 
      System.out.println("Text " + xmlResourceParser.getText()); 
     } 
     eventType = xmlResourceParser.next(); 
    } 

感謝大家的幫助

-1

試試這個

final String xml ="<?xml version=\"1.0\"?><thesaurus><word name=\"let\"><synonyms>allow</synonyms></word></thesaurus>"; 

    final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = null; 
    try { 
     builder = builderFactory.newDocumentBuilder(); 
     final Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes())); 
     final XPath xPath = XPathFactory.newInstance().newXPath(); 
     final Object result = xPath.compile("/thesaurus/word/synonyms").evaluate(xmlDocument, XPathConstants.NODESET); 
     NodeList nodes = (NodeList) result; 

     for (int h = 0; h < nodes.getLength(); h++) { 
      final Node node = nodes.item(h); 
      final NodeList venueChildNodes = node.getChildNodes(); 
      System.out.println(node.getChildNodes().item(0).getTextContent()); 
     } 

    } catch (ParserConfigurationException | SAXException | IOException e) { 
     e.printStackTrace(); 
    } catch (XPathExpressionException e) { 
     e.printStackTrace(); 
    } 
+0

我不明白爲什麼XML字符串中開始了。我需要從文檔中讀取它。如果我可以從文件中讀取XML,那麼我願意嘗試NodeList。我怎樣才能做到這一點?謝謝! – 26hmkk

+2

你能解釋一下你在這裏解決了這個問題嗎? – nweg

0

當你調用xml.getText()的XML解析器當前指向在START_TAG,而不是在內容。調用xml.next()允許的getText()返回內容:

if (xml.getName() != null && xml.getName().equalsIgnoreCase("synonyms")) { 
    xml.next(); 
    String synonym = xml.getText(); 
    Log.v(TAG, String.valueOf(synonym)); 
    notFound = false; //found 
} 

可以驗證迭代器的位置。比如用:

if (xml.getEventType() == XmlPullParser.TEXT) { 
    // iterator is at content 
} 
+0

此代碼也不起作用。 – 26hmkk