Hey,
I was reading over Jubba’s "XML Parsing using PHP (Intermediate) tutorial and had a little question. I was trying to add a new entry. In the tutorial, we have: (i left out the whole xml, i think this is all you need)
<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>
so i added it to have a source tag also, like the following:
<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>
<source>Jubba</source>
</story>
I edited the php to be:
<?php
$xml_file = "xml_intermediate.xml";
$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";
$xml_source_key = "*NEWS*STORY*SOURCE";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description, $source;
}
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, $xml_source_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;
break;
case $xml_source_key:
$story_array[$counter]->source = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for($x=0;$x<count($story_array);$x++){
echo " <h2>" . $story_array[$x]->headline . "</h2>
";
echo "
";
echo " <i>" . $story_array[$x]->description . "</i>
";
echo "
";
echo "\<b>" . $stroy_array[$x]->source . "</b>
";
}
?>
</body>
</html>
For some reason, when I load the page, all i see is a blank page. Any ideas?
Thank you.