2014-10-27 46 views
0

如何解析XML这样(一个XML是有一个内部标记)。我没有得到simpleChoice节点,当我尝试打印它显示总是空白,任何人都可以帮助我。Android XML解析没有出去放

<itemBody> 
    <p>Look at the text in the picture.</p> 
    <p> 
     <img src="images/sign.png" alt="NEVER LEAVE LUGGAGE UNATTENDED"/> 
    </p> 
    <choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1"> 
     <prompt>What does it say?</prompt> 
     <simpleChoice identifier="ChoiceA">You must stay with your luggage at all times.</simpleChoice> 
     <simpleChoice identifier="ChoiceB">Do not let someone else look after your luggage.</simpleChoice> 
     <simpleChoice identifier="ChoiceC">Remember your luggage when you leave.</simpleChoice> 
    </choiceInteraction> 
</itemBody> 

我曾试图 -

  NodeList nodeList = doc.getElementsByTagName("itemBody"); 
     XMLParser parser = new XMLParser(); 

     for (int i = 0; i < nodeList.getLength(); i++) 
     { 
      Element e = (Element) nodeList.item(i); 
      Log.i("TAG","<p>: " + parser.getValue(e, "p")); 

      //Log.i("TAG","Count: " + parser.getValue(e, "choiceInteraction")); 
      //Log.i("TAG","Count: " + parser.getValue(e, "p")); 
     } 

     NodeList nodeChoiceInteractionList = doc.getElementsByTagName("choiceInteraction"); 
     for (int j = 0; j < nodeChoiceInteractionList.getLength(); j++) 
     { 
      Element eChoiceInteractionList = (Element) nodeChoiceInteractionList.item(j); 
      Log.i("TAG","<prompt>: " + parser.getValue(eChoiceInteractionList, "prompt")); 
     } 

     NodeList simpleChoiceList = doc.getElementsByTagName("simpleChoice"); 
     for (int j = 0; j < simpleChoiceList.getLength(); j++) 
     { 
      Element eSimpleChoice = (Element) simpleChoiceList.item(j); 
      Log.i("TAG","<simpleChoice>: " + parser.getValue(eSimpleChoice, "simpleChoice")); 
     } 

回答

1

你像ItemBody的孩子找,所以你需要遍历nodeChoiceInteractionList寻找simpleChoice节点,像你这样的提示节点做。

也尝试在日志打印的时候,这代码迭代给你一个想法

for (int j = 0; j < nodeChoiceInteractionList.getLength(); j++) 
    { 
     Element eChoiceInteractionList = (Element) nodeChoiceInteractionList.item(j); 
     Log.i("TAG","<prompt>: " + parser.getValue(eChoiceInteractionList, "prompt")); 
     >>Log.i("TAG","<simpleChoice>: " + parser.getValue(eSimpleChoice, "simpleChoice")); 
     >>Log.d("Iterate Number",j); 
    }