Hello, I am having difficulty with parsing xml. It seems like it is working right, but it only displays 1 post, instead of 5.
the xml page I am trying to read is http://www.cpsc.gov/cpscpub/prerel/prerel.xml
My code is
<?
/*****COUNTER SET TO A NUMBER OF ITEMS TO DISPLAY:******/
$cc=5;
/*****URL of your XML file*****/
$url = "http://www.cpsc.gov/cpscpub/prerel/prerel.xml";
//ELEMENTS TO RETRIEVE - YOU MUST KNOW THE TAG NAMES OF EACH ELEMENT:
$title='';
$description='';
$pubDate='';
$link='';
$guid='';
//Counter to start counting the number of items
$cz=0;
$data = file_get_contents($url);
$depth = array();
//The dataset will be stored inside this variable
$txt='';
function startElement($parser, $name, $attrs)
{
//we use global variables to keep track of each element while parsing the whole document
global $cc,$cz,$txt;
global $depth;
global $tagname;
global $title,$description,$pubDate,$link,$guid;
if($cz < $cc)
{
/* if the level is the first, depending on your xml structure, you may need to change the value*/
if($depth[$parser] == 1)
{
//at this level we have a NEW ITEM -> we increment to items' counter
if($cz++ >= 1)
{
/*We display the dataset in the order we wish (see below to apply a different design)*/
$txt.=$title.$description.$pubDate.$link.$guid;
$title='';
$description='';
$pubDate='';
$link='';
$guid='';
}
}
else
{
//we store the current element
$tagname= $name;
}
}
//one level deeper
$depth[$parser]++;
}
function endElement($parser, $name)
{
global $depth;
global $tagname;
$tagname='';
$depth[$parser]--;
}
function characterData($parser, $ddata)
{
global $tagname;
global $cc,$cz,$txt;
global $title,$description,$pubDate,$link,$guid;
/*For each tag name we receive we only extract the wanted ones (for instance here we do not want to display the DATE)
You can apply different layout to the data
*/
if($cz < $cc)
{
switch($tagname)
{
case 'TITLE':$title= '<tr><td style="width:40%;background-color:#fafafa;border-right:1px
solid black" valign="top"><h3>'.utf8_encode($ddata).'</h3>';break;
case 'AUTHOR':$description= '<u>Author</u>: '.utf8_encode($ddata).'</td></tr>';break;
case 'Date':$pubDate= '<tr><td>'.utf8_encode($ddata).'</td></tr>';break;
case 'LINK':$link= '<tr><td>'.utf8_encode($ddata).'</td></tr>';break;
case 'GUID':$guid= '<tr><td>'.utf8_encode($ddata).'</td></tr>';break;
default: break;
}
}
}
/*everything is triggered here - we parse the xml file and call the startElement & endElement functions, then the
characterData function to read all content - the xml_parse line launches the procedure taking care of the arguments*/
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!xml_parse($xml_parser, $data))
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));
xml_parser_free($xml_parser);
//We store the last elements
$txt.=$title.$description.$pubDate.$link.$guid;
/*echo the result inside a table (you may write it into a file)*/
$txt= '<table cellpadding="5" style="border:1px solid black">'.$txt.'</table>';
echo $txt;
?>
Does anyone see anything goofy that I am not seeing? My output is here:
http://www.theinterfaceplace.com/inrecall/index_test.php
Thanks!!!