Why is this code returning blank data to an array?
Posted: Sun Oct 15, 2006 1:08 pm
I know I've been advied on here many time to use return for transporting variables but I'm confortable with globals at the moment which is why I've used them in the below code.
The code is to try and pass all data parsed from an XML file into an array.
The code is to try and pass all data parsed from an XML file into an array.
Code: Select all
if (!($xmlparser = xml_parser_create())) { die ("Connect create parser"); }
function start_tag($parser, $name, $attribs) {
global $name;
}
function tag_contents($parser, $data) {
global $container;
global $tagCount;
global $name;
$container[$tagCount][$name] = $data;
}
function end_tag($parser, $name) {
global $tagCount;
$tagCount += 1;
}
global $container;
print_r($container);
xml_set_element_handler($xmlparser, "start_tag", "end_tag");
xml_set_character_data_handler($xmlparser, "tag_contents");
$filename = "data.xml";
if (!($fp = fopen($filename, "r"))) { die("Cannot open ". $filename); }
while ($data = fread($fp, 4096)) {
$data = eregi_replace(">"."[[]]+"."<","><", $data);
if (!xml_parse($xmlparser, $data, feof($fp))) {
$reason = xml_error_string(xml_get_error_code($xmlparser));
$reason .= xml_get_current_line_number($xmlparser);
$reason .= xml_get_current_line_number($xmlparser);
die ($reason);
}
}
xml_parser_free($xmlparser);