I'm pretty new to DOM and XML in general. I've been doing pretty well until tonight.
My code works great when no CDATA tags are present in the <answer_response> tags,
but when CDATA is there, the server crashes.
I've got a little XML doc that looks something like this:
<test>
<title>
<![CDATA[Wrap Accounts]]>
</title>
<introduction>
<![CDATA[ Nothing ]]>
</introduction>
<question>
<question_statement>
A silly question
</question_statement>
<possible_answer is_right_answer="F">
<answer_text>
Answer A
</answer_text>
<answer_response>
<![CDATA[
Wrong
]]>
</answer_response>
</possible_answer>
<possible_answer is_right_answer="T">
<answer_text>
Answer B
</answer_text>
<answer_response>
<![CDATA[
Right
]]>
</answer_response>
</possible_answer>
</question>
</test>
Now, the code where I'm trying to access these elements looks like this:
get the content of a particular node
function find_content($parent,$name)
{
echo "GOT HERE!";
$nodes = $parent->children();
while($node = array_shift($nodes))
if ($node->name == $name)
return get_content($node);
return "";
}
iterate through an array of nodes
looking for a text node
return its content
function get_content($parent)
{
$nodes = $parent->children();
while($node = array_shift($nodes))
if ($node->type == XML_TEXT_NODE)
return $node->content;
return "";
}
$possible_answer_nodes = xpath_eval($ctx,"//test/question[$i]/possible_answer");
foreach($possible_answer_nodes->nodeset as $poss_node)
{
if ($poss_node->type == XML_TEXT_NODE)
continue;
$answer = addslashes(find_content($poss_node,"answer_text"));
$response[$letter] = addslashes(find_content($poss_node,"answer_response"));
}
WHen the server hits the find_content routine, things explode. If there are no CDATA tags, everything works dandy.
Am I just going about this the wrong way?
What would be a more appropriate way to access the various elements that make up a <possible_answer> that might or might not contain CDATA tags?