PDA

View Full Version : PHP, XML parsing, OOP and Function



blitzen
01-03-2012, 10:37 AM
This works if you remove the function lines (first and last lines).
I wrapped it in 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 this as a function for a more complex project that will call it as needed. I'll need an array of all the $myarray['headline']['description'] returned which I can create at the end (replace the echo statements - the echos are there for testing.).

Can you help me make this work as a function? I can figure out how to get the array I need returned. I don't know why the embedded class and functions won't work.


<?
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

Original code from http://www.kirupa.com/web/xml_php_parse_intermediate.htm

gary
01-05-2012, 06:26 PM
When you threw the code into a function you lost scope on some key variables.

Add this line after the data block:
global $xml_headline_key, $xml_description_key, $story_array, $counter;

williamc
01-06-2012, 03:34 AM
also, do not 'echo' the output, but 'return' it.

change all of your echo statements into one string using something like $output .= in place of the echo's, then at the bottom return $output;

blitzen
01-06-2012, 10:34 AM
Thanks a bunch! You guys are the greatest.

I asked some other forum touting to be a php expert and they could not fix this. They only repeated what I already posted (and knew). I guess I'll bug you guys in the future for that real expert advice.