472,145 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

trying to parse xml

Hi,

I'm trying to parse an xml file into an array tree. From the PHP site
in the comments, I got this code. But it doesn't work for me. It's saying
that the passed variable is not an array or object in the call end($stack),
in the first function startElement(). Anyone know why that would be?
$stack is an array, as far as I can tell.

Thanks
B
<?php

$file = "data.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[] = &$tree;
function startElement($parser, $name, $attrs) {
global $depth;
global $stack;
global $tree;

$element = array();
foreach ($attrs as $key =$value) {
$element[strtolower($key)]=$value;
}

end($stack);
$stack[key($stack)][strtolower($name)] = &$element;
$stack[strtolower($name)] = &$element;

$depth++;
}

function endElement($parser, $name) {
global $depth;
global $stack;

array_pop($stack);
$depth--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
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);
$tree = end(end($stack));
echo "<pre>";
print_r($tree);
echo "</pre>";

?>
Dec 19 '07 #1
3 1626
On Dec 19, 3:06 pm, "Bint" <b...@csgs.comwrote:
Hi,

I'm trying to parse an xml file into an array tree. From the PHP site
in the comments, I got this code. But it doesn't work for me. It's saying
that the passed variable is not an array or object in the call end($stack),
in the first function startElement(). Anyone know why that would be?
$stack is an array, as far as I can tell.

Thanks
B

<?php

$file = "data.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[] = &$tree;

function startElement($parser, $name, $attrs) {
global $depth;
global $stack;
global $tree;

$element = array();
foreach ($attrs as $key =$value) {
$element[strtolower($key)]=$value;
}

end($stack);
$stack[key($stack)][strtolower($name)] = &$element;
$stack[strtolower($name)] = &$element;

$depth++;

}

function endElement($parser, $name) {
global $depth;
global $stack;

array_pop($stack);
$depth--;

}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");

}

while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
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);
$tree = end(end($stack));
echo "<pre>";
print_r($tree);
echo "</pre>";

?>
maybe its just me, but where is this function "end" set up?

Dec 19 '07 #2
end is a php function, it points the current position of an array to its
end.
Dec 19 '07 #3
Bint wrote:
I'm trying to parse an xml file into an array tree. From the PHP site
in the comments, I got this code. But it doesn't work for me. It's
saying that the passed variable is not an array or object in the call
end($stack), in the first function startElement(). Anyone know why that
would be? $stack is an array, as far as I can tell.
Not the most elegant code, but it seems to work here using PHP 5.2.5.
Which version of PHP are you using?

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 12 days, 20:32.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Martin | last post: by
4 posts views Thread by Chuck Haeberle | last post: by
3 posts views Thread by Bruce D | last post: by
2 posts views Thread by msnews.microsoft.com | last post: by
1 post views Thread by paulsendave | last post: by
2 posts views Thread by =?Utf-8?B?Q2hyaXM=?= | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.