I’ve looked around and surprisingly can’t find a topic that has delt with this, so I’ll give this a shot. I was trying to simulate/modify the php/xml code located at this tutorial. (http://www.kirupa.com/web/xml_php_parse_intermediate.htm) I’ve gone in and applied my styles to the php and added 2 additional variables/items to the php and xml.
The issue here is that the page is displaying no information in the new tags. It’s not giving me an error message, but It’s not echoing anything. The php is writing the correctly formatted html, but it’s not calling the new tags from the xml.
It seems like this could be an easy bone-head mistake, but I just can’t see it. Any thoughts?
Here’s the XML:
<?xml version="1.0"?>
<news>
<story>
<headline>Godzilla Attacks LA!</headline>
<dateLog>December 6th 2005</dateLog>
<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>Phil Jayhan's Death Star Update</headline>
<dateLog>December 6th 2005</dateLog>
<description>After years of contracting set-backs and searching for hard-to-get parts, Mr. Jayhan is nearing completion on his world-destroying Death-Star. We are all looking forward to the future enslaving and erradication of the human race. </description>
</story>
<story>
<headline>Bigfoot Spoted at M.I.T. Dining Area</headline>
<dateLog>December 6th 2005</dateLog>
<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>
<dateLog>December 6th 2005</dateLog>
<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>
<dateLog>December 6th 2005</dateLog>
<description>Uhhhmmm... No comment really... just a little creepy to see them together...</description>
</story>
<story>
<headline>Ahmed's Birthday Extravaganza!</headline>
<dateLog>December 6th 2005</dateLog>
<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>
Andthe php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
// Simple enough, the location of the XML file
$xml_file = "info.xml";
// These are both keys that we will use later.
$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_dateLog_key = "NEWS*STORY*dateLog";
$xml_image_key = "NEWS*STORY*IMAGE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";
// An array for storing our information. An array is nice to use here
// because it allows us to parse the XML and then temporarily forget about it
// allowing use greater freedom to edit and maniplulate the output.
$story_array = array();
// A counter that will come into use later.
$counter = 0;
// A simple class that will make our life easier. We could use an
// associative array as well, but I prefer to just write up the class. =)
class xml_story{
var $headline, $dateLog, $image, $description;
}
// Once again, this is what we want our parser to do when it reaches a start tag
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
// Same thing here as well. This tells the parser what to do when it trips over an end tag.
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
// When the parser hits the contents of the tags it will perform this function.
// This will all be explained word for word in the tutorial
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_dateLog_key, $xml_image_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_dateLog_key:
$story_array[$counter]->dateLog = $data;
break;
case $xml_image_key:
$story_array[$counter]->image = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
}
}
// Creates the parser
$xml_parser = xml_parser_create();
// Sets the element handlers for the start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");
// Sets the data handler, same as before...
xml_set_character_data_handler($xml_parser, "contents");
// Opens the file or gives an error message
$fp = fopen($xml_file, "r") or die("Could not open file");
// Reads the file or gives an error message
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
// This if statement is exactly the same as before. It parses the xml document
// according to the functions we have defined; and it returns an error message
// if the parsing fails
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
// Frees up the memory
xml_parser_free($xml_parser);
// Closes the file
fclose($fp);
?>
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?
// A simple for loop that outputs our final data.
for($x=0;$x<count($story_array);$x++){
echo " <div id=\"news\">
";
echo " <p><span class=\"header1\">" . $story_array[$x]->headline . "</span><br>
";
echo " <span class=\"header2\">" . $story_array[$x]->dateLog . "</span><br>
";
echo " <img id=\"thumbPic\" src=\"http://www.triplogixpromotions.com/bernina/images/" . $story_array[$x]->image . "\" alt=\"Come in, it's open!\">
";
//echo " <h2>" $story_array[$x]->headline . "</h2>
";
//echo " <br />
";
echo " " . $story_array[$x]->description . "</p>
";
echo " <hr noshade>
";
echo " </div>
";
}
?>
</body>
</html>