Jdsflash Posted November 30, 2009 Share Posted November 30, 2009 This page is not parsing the xml correctly. count($story_array) this is 0 and it should be 2 to get the items to display. Well at leist is think this is the issue. link to the page. http://www.flashmajic.com/Google/index11.php <?php // Simple enough, the location of the XML file $xml_file = "../gadgets.xml"; // These are both keys that we will use later. $xml_headline_key = "*Gadgets*Site*Title"; $xml_description_key = "*Gadgets*Site*DESCRIPTION"; $xml_link_key = "*Gadgets*Site*LINKS"; // 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 $Title, $description, $links; } // 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_description_key, $xml_link_key, $counter, $story_array; switch($current_tag){ case $xml_headline_key: $story_array[$counter] = new xml_story(); $story_array[$counter]->Title = $data; break; case $xml_description_key: $story_array[$counter]->description = $data; break; case $xml_link_key: $story_array[$counter]->links = $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"> <div align="center"><h2>Public Surveys</h2></div> <table width="90%"> <tr> <td valign="top"> <?php // A simple for loop that outputs our final data. echo count($story_array); for($x=0;$x<count($story_array);$x++){ echo "<h3><a href=".$story_array[$x]->links.">".$story_array[$x]->Title."</h3></a>"; echo "<h4>".$story_array[$x]->description."</h4>"; } ?> </td> <td valign="top"></td> </tr> </table> </body> </html> My xml page <Gadgets> <Site> <Title>My title </Title> <Image>http://www.google.com</Image> <description>My description</description> <links>http://www.google.com</links> </Site> <Site> <Title>My title</Title> <Image>http://www.google.com</Image> <description>My description.</description> <links>http://www.google.com</links> </Site> <Site> </Gadgets> Link to comment https://forums.phpfreaks.com/topic/183376-parsing-xml-in-php-problem-newbe-here/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.