returned xml array troubles
Posted: Mon Jan 30, 2006 2:45 pm
Hey guys, I'm working through this tutorial from the Zend.com. I'm trying to make the tutorial practical for a real life scenario. Here's a sample XML:
When I run the parser code below, the first "item" title would be "My Site Article one". Thus incorrectly combining the site title and the first item's title. The second item (if the example above had one) would out put as expected -- "Article Two".
The question is how do I get the parser to make a two tiered array? Something like:
I'm not sure where to get a variable to track what level I'm on or if I need a new array or whatever.
Thanks for your help
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>My Site</title>
<link>http://www.mysite.com</link>
<description>Visit my site</description>
<language>en-us</language>
<copyright>(C) your grandma</copyright>
<image>
<title>Greetings</title>
<link>http://www.mysite.com/</link>
<url>http://www.mysite.com/images/logo.jpg</url>
</image>
<item>
<title>Article one</title>
<link>http://www.mysite.com/article.html</link>
<pubDate>Sat, 28 Jan 2006 11:25:00 EST</pubDate>
<description><![CDATA[<p>Read this article to find out how</p>]]></description>
<category>How to</category>
<guid isPermaLink="false">http://www.mysite.com/3172.html</guid>
</item>
</channel>The question is how do I get the parser to make a two tiered array? Something like:
Code: Select all
array( 'site description',
array( array( 'item'),
array('item')
array('item')
);
);Code: Select all
<?php
// the xml file we want to parse
$xmlSource="rssTest.xml";
$title="";
$link="";
$pubDate="";
$description="";
$category="";
$currentElement=""; //holds the name of the current element
$depth = array();
$items=array(); //array to hold all the movie data
function startElement($parser,$name,$attr){
$GLOBALS['currentElement']=$name;
}
function endElement($parser,$name){
$elements=array('title', 'link', 'pubDate', 'description', 'category');
if(strcmp($name,"item")==0){
foreach($elements as $element){
$temp[$element]=$GLOBALS[$element];
}
///HERE's WHERE IT GOES INTO THE ARRAY
$GLOBALS['items'][]=$temp;
$GLOBALS['title']="";
$GLOBALS['link']="";
$GLOBALS['pubDate']="";
$GLOBALS['description']="";
$GLOBALS['category']= "";
}
}
function characterData($parser, $data) {
$elements=array('title', 'link', 'pubDate', 'description', 'category');
foreach ($elements as $element) {
if ($GLOBALS["currentElement"] == $element ) {
$GLOBALS[$element] .= $data;
}
}
}
function parseFile(){
global $xmlSource,$items;
/*Creating the xml parser*/
$xml_parser=xml_parser_create();
/*Register the handlers*/
xml_set_element_handler($xml_parser,"startElement","endElement");
xml_set_character_data_handler($xml_parser,"characterData");
/*Disables case-folding. Needed for this example*/
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,false);
/*Open the xml file and feed it to the parser in 4k blocks*/
if(!($fp=fopen($xmlSource,"r"))){
die("Cannot open $xmlSource ");
}
$x =0;
while(($data=fread($fp,4096))){
if(!xml_parse($xml_parser,$data,feof($fp))){
die(sprintf("XML error at line %d column %d ",
xml_get_current_line_number($xml_parser),
xml_get_current_column_number($xml_parser)));
}
$x++;
}
/*Finish ! we free the parser and returns the array*/
xml_parser_free($xml_parser);
return $items;
}//end parseFile()
/***************************************************************************************************
Calling the parseFile() and getting the result out in a simple html-table
***************************************************************************************************/
$result=parseFile();
print '<table border="1">';
foreach($result as $arr){
/*check on movieId to see if we reached a new movie.
* If so we print out the movieName
*/
if(strcmp($id,$arr["title"])!=0){
print '
<tr>
<td colspan="3"><b><a href="'.$arr['link'].'">'.$arr["title"].'</a></b></td>
</tr>
<tr>
<td colspan="3">'.$arr["description"].'</td>
</tr>
';
}
}
print '</table>';
?>