XML Parsing code not working
Posted: Tue Sep 05, 2006 8:41 pm
I know this is a lot of code, but I just can't seem to find where I went wrong. It just displays a blank page. Also, let it be known that this is a -major- hack from someone else's code.
Here's the XML
Thanks in advance for any advice you could give.
Code: Select all
<?php
if (!($fp=@fopen("./contact.xml", "r"))) die ("Couldn't open XML.");
$problemCount=0;
$problemData=array();
$state='';
function startElementHandler ($parser,$name,$attrib){
global $problemCount;
global $problemData;
global $state;
switch ($name) {
case $name=="QUESTION" : {
$problemData[$problemCount]["type"] = $attrib["TYPE"];
$problemData[$problemCount]["difficulty"] = $attrib["DIFFICULTY"];
break;
}
default : {$state=$name;break;}
}
}
function endElementHandler ($parser,$name){
global $problemCount;
global $problemData;
global $state;
$state='';
if($name=="PROBLEM") {$problemCount++;}
}
function characterDataHandler ($parser, $data) {
global $problemCount;
global $problemData;
global $state;
if (!$state) {return;}
if ($state=="ASK") { $problemData[$problemCount]["ask"] = $data;}
if ($state=="ANSWER") { $problemData[$problemCount]["answer"] = $data;}
}
if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler( $xml_parser, "characterDataHandler");
while( $data = fread($fp, 4096)){
if(!xml_parse($xml_parser, $data, feof($fp))) {
break;}}
xml_parser_free($xml_parser);
?>
<html>
<head><title>Jimmy</title>
</head>
<body>
<?php
for ($i=0;$i<$problemCount; $i++) {
echo $problemData[$i]["answer"];
}
?>
</table>
</body>
<html>Code: Select all
<PROBLEMS>
<PROBLEM>
<QUESTION TYPE="MATH" DIFFICULTY="EASY" />
<ASK>what is one plus one</ASK>
<ANSWER>two</answer>
</PROBLEM>
</PROBLEMS>Thanks in advance for any advice you could give.