This works until I put it into a function, and it broke. I spent hours on it and am not an expert OOP PHP programmer. It just may take you a few minutes to adjust it.
Your help is greatly appreciated in getting it to work.
I need it in a function for a more complex project that will call it as needed. I'll need an array of all the ['headline']['description'] returned which I can make at the end (replace the echo statements).
<?
function parseMyXML() {
$data = '<?xml version="1.0"?>';
$data .= <<<qq
<news>
<story>
<headline> Godzilla Attacks LA! </headline>
<description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description>
</story>
<story>
<headline> Bigfoot Spotted at M.I.T. Dining Area </headline>
<description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing. </description>
</story>
<story>
<headline> London Angel Saves England </headline>
<description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her. </description>
</story>
<story>
<headline> Six-eyed Man to be Wed to an Eight-armed Woman </headline>
<description>Uhhhmmm... No comment really... just a little creepy to see them together... </description>
</story>
<story>
<headline> Ahmed's Birthday Extravaganza! </headline>
<description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides. </description>
</story>
</news>
qq;
$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
xml_parse($xml_parser, $data);
xml_parser_free($xml_parser);
echo '
<html><head>
<title>My Parser</title>
</head>
<body>
';
for($x=0;$x<count($story_array);$x++){
echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n";
echo "\t\t\n";
echo "\t<i>" . $story_array[$x]->description . "</i>\n";
}
echo '</body></html>';
} // end function
Code from http://www.kirupa.com/web/xml_php_parse_intermediate.htm