I have an XML file with an irregular amount of child nodes, and would like to display the question and child nodes. The display for the questions is correct, but I am getting stuck on properly displaying the child nodes.
<Survey>
<Question ID="1">
<QuestionText>Question Number 1</SurveyQuestionText>
<AnswerText>Answer 1</SurveyAnswerText>
<AnswerText>Answer 2</SurveyAnswerText>
<AnswerText>Answer 3</SurveyAnswerText>
<AnswerText>Answer 4</SurveyAnswerText>
<AnswerText>Answer 5</SurveyAnswerText>
</Question>
<Question ID="2">
<QuestionText>Question Number 2</SurveyQuestionText>
<AnswerText>Answer 1</SurveyAnswerText>
<AnswerText>Answer 2</SurveyAnswerText>
<AnswerText>Answer 3</SurveyAnswerText>
</Question>
<Question ID="3">
<QuestionText>Question Number 3</SurveyQuestionText>
<AnswerText>Answer 1</SurveyAnswerText>
<AnswerText>Answer 2</SurveyAnswerText>
<AnswerText>Answer 3</SurveyAnswerText>
<AnswerText>Answer 4</SurveyAnswerText>
</Question>
</Survey>
Here is my code so far
$xml_url = "questions.xml";
$xml = simplexml_load_file($xml_url);
$x = 1;
foreach($xml->children() as $question) {
echo $x . ".) " . $question->QuestionText . "
";
$numAnswers = $question->AnswerText->count();
echo "count: " . $numAnswers . "
";
//insert code for display AnswerText
$x++;
}
Here is my output so far:
1.) Question Number 1
count: 5
2.) Question Number 2
count: 3
3.) Question Number 3
count: 4