multiple child nodes in xml

FORD JR., CURT CFORD at mailbox.sc.edu
Fri Jan 10 09:06:28 EST 2014


I’m stuck on getting the values in some multiple child nodes in xml… trying to base the code on the sample at http://lessons.runrev.com/s/lessons/m/4071/l/7011-how-to-read-in-data-from-an-xml-file .

I’m getting the list of prompts that I need, but I can’t get the list of answers: one way I’ve tried gets the first answer only for each question, three times; another try (commented out) gave blanks for the answers. Am I missing something basic in how to set up the inner repeat loop, or the syntax for getting the answers in revXMLNodeContents?

My xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<quiz>
    <items>
        <item>
            <prompt>What is the capital of Russia?</prompt>
            <answers>
                <answer>Moscow.</answer>
                <answer>Omsk.</answer>
                <answer>Tomsk.</answer>
            </answers>
        </item>
        <item>
            <prompt>How's the weather?</prompt>
            <answers>
                <answer>Pretty cold.</answer>
                <answer>Not bad.</answer>
                <answer>Too hot.</answer>
            </answers>
        </item>
    </items>
</quiz>


Here’s what the command is like now:

private command processQuizTree pTree

   local tItems
   put revXMLChildNames(pTree, "quiz/items", return, "item", true) into tItems
   answer "tItems is" && tItems
   --answer "tItems[1] is " & tItems[1]
   --answer "line 1 of tItems is " & line 1 of tItems

   local tListOfPrompts
   repeat for each line tItem in tItems

      put revXMLNodeContents(pTree, "quiz/items/" & tItem & "/prompt") into tCurrentPrompt
      answer "tCurrentPrompt is " & tCurrentPrompt
      put tCurrentPrompt & return after tListOfPrompts

      --get the child names of the <answers> node
      put revXMLChildNames(pTree, "quiz/items/item/answers", return, "answer", true) into tAnswers
      answer "tAnswers is" && tAnswers

      local tListOfAnswers
      repeat for each line tAnswer in tAnswers
         --gets first answer only, 3 times??
         put revXMLNodeContents(pTree, "quiz/items/" & tItem & tAnswer & "/answers/answer") into tCurrentAnswer
         answer "tCurrentAnswer is " & tCurrentAnswer
         put tCurrentAnswer & return after tListOfAnswers

         --gets blank item? 3 times
         --put revXMLNodeContents(pTree, "quiz/items/" & tItem & tAnswer & "/answers") into tCurrentAnswer
         --answer "tCurrentAnswer is " & tCurrentAnswer
         --put tCurrentAnswer & return after tListOfAnswers
      end repeat

   end repeat

   delete the last char of tListOfItems --trailing return

   answer "tListOfPrompts is" && tListOfPrompts
   answer "tListOfAnswers is" && tListOfAnswers

end processQuizTree

-thanks for any help -

Curt



More information about the use-livecode mailing list