473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1694
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John T | last post by:
I am trying to make a function that takes an optional parameter that gets passed by reference. Here is the first line of my function definition: function funQueryDatabase($strQuery,...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
0
by: Martin | last post by:
I'm distributing stand-alone HTML forms for users to complete and e-mail/snail-mail back to me. (For security and budget reasons, receiving the form data to a web- server is not an option.) Then...
9
by: florent | last post by:
I'm trying to parse html documents from the web, using the HTMLParser class of the HTMLParser module (python 2.3), but some web documents are not fully valids. When the parser finds an invalid tag,...
4
by: Chuck Haeberle | last post by:
I have an interesting regular expression challenge for someone more experienced with them than I for a data layer class... I need an expression to search a SQL statement (any type, SELECT INSERT...
3
by: Bruce D | last post by:
I'm new to .NET and I have a error that is coming up but it's not telling me where the error is coming from (no file). Is that because I'm doing something wrong...or is it a type of error that has...
2
by: msnews.microsoft.com | last post by:
I am currently trying to follow the Que Publishing exam guide to the Microsoft Exam 70-320. I followed the guide to a tee, but when I try to add a web reference to my server side soap extension it...
1
by: paulsendave | last post by:
What a mix! Whilst I grep around for the first set of problems, I thought I'd ask any generous members of this PHP community for clues... I've started by trying out the instructions at:...
2
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I am usign the code below to find the current row and cell. Which works OK. Then hwat I want to do is get the value of each other cell in the row. So below I am trying to get at least one values....
3
by: compuv | last post by:
I'm obtaining a weird message when I try to add the capability to write to a csv file into the formmail script that I'm using. Everything is fine initially and the email comes through to me perfectly...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.