Hi
I’m parsing an xml feed (at the moment using the script below) and I want to echo out the data inside tags but only if it’s not more xml to parse. This is easy in the contents functions I just need to strip space and check the length is greater than 0 but how do I stop the starttag and endtag from echoing as well? Any ideas on how to do this?
I know I can do this using a xml to array parsing function but then I have to parse the array again to get it into the format I want which seems silly (and twice as long).
Thanks in advance
Garrett
<?php
$file = "file.xml";
//functions to pase the xml content
function contents($parser, $data)
{
//strip all excess white space and echo data if it's longer than 0
echo $data;
}
function starttag($parser, $data)
{
echo $data.":";
}
function endtag($parser, $data)
{
echo "<br />";
}
//functions to pase the xml content
//create a parser object
$xml_parser = xml_parser_create();
//put this in front and after each bit of data
xml_set_element_handler($xml_parser, "starttag", "endtag");
//parse the contents
xml_set_character_data_handler($xml_parser, "contents");
//open the file with read permissions
$fp = fopen($file, "r");
//read the file
$data = fread($fp, 80000);
//parse or die
if(!(xml_parse($xml_parser, $data, feof($fp))))
{
die("Error on line " . xml_get_current_line_number($xml_parser));
}
//free the parser
xml_parser_free($xml_parser);
//close the file
fclose($fp);
?>