XML to Array
Filed under: PHP — John @ 8:51 pm March 20, 2005
I had a problem were i couldn’t get my xml feeds into an array so i got these functions thatdo it for you and make your life so much easyer to handle, makes putting xml feeds into a db so much easyer.
$url - is the location of the xml feed.
and $data is the xml into the array.
enjoy
Removed the large source, it is avalible through the link below, wasn’t validating as xhtml
Plain text source : http://whoooop.co.uk/xml.txt
use this snippet post a comment of any improvements you make and were you used it.


Thanks for such a useful code… That is waht I needed…
Comment by Fatih — March 23, 2005 @ 5:29 pm
Fantastic. Fast and exactly what I was looking for. Thanks!
Comment by Matt — March 28, 2005 @ 10:49 pm
I think this is great, however it seems to be missing one attibute function. When you load an xml into it where the father node has attributes AND childNodes it gets the value of the childNodes into array, however if it’s just nodes with attributes it puts the attributes into the array. It would be great if it somehow did both. An example is this flickr xml
Comment by Michael — March 31, 2005 @ 3:15 am
The script don’t work in PHP5.
Comment by cristi — March 31, 2005 @ 6:07 pm
theres an easy fix for it just can’t for the life of me think what it was.
Never mind hopefully someone will post how to
Comment by CaShY — March 31, 2005 @ 6:16 pm
basically
SNIP]
function GetXMLTree ($xmldata)
{
$attributes =array();
// we want to know if an error occurs
iniset (’trackerrors’, ‘1′);
[SNIP]
makes it work in php5
Comment by CaShY — March 31, 2005 @ 6:20 pm
Thanks, CaShY. You help me very much.
Comment by cristi — April 2, 2005 @ 8:47 am
any idea of how to fix the problem that Michael pointed out ?
Thanx
Comment by ibrahim — April 19, 2005 @ 10:40 am
with this simple xml file , i get all attributes but not the name of the city
exemple :
BC
516521
-1212891
-800
1
Comment by ibrahim — April 19, 2005 @ 10:41 am
Very useful code but I had problems with empty xml files, for example:
<NAME> </NAME>
I fix the problem changing the call to the funtion GetChildren in the main function….
—– original ——
$result [$vals [$i][’tag’]] = arraymerge ($attributes, GetChildren ($vals, $i, ‘open’));
—– fixed ——–
$result [$vals [$i][’tag’]] = arraymerge ($attributes, GetChildren ($vals, $i, $vals[$i][’type’]));
Sorry for my poor english and thanks for the code.
Comment by Jose — April 22, 2005 @ 10:02 am
wow,wonderful~
thx u very much,that’s what I needed
Comment by simon — May 20, 2005 @ 7:59 pm
Wow, worked like a charm ! Thanks a lot, mate - i just spent an hour trying to get an XML feed to work and needed 5 minutes with your script
Comment by Watches — May 23, 2005 @ 11:44 pm
very useful and very nice code, thanks, CaShy!
a little problem:
somehow I need to keep original aspect of the data in my xml file to output, however, when I change
xmlparsersetoption ($parser, XMLOPTIONSKIPWHITE, 1)
into
xmlparsersetoption ($parser, XMLOPTIONSKIPWHITE, 0)
and it can’t work.
pls fix this problem, I’ll be still waiting. Thanx again(and sorry for my poor English).
Comment by cid73 — May 26, 2005 @ 11:09 am
Very useful script - thanks.
I have one small problem with the script though.
If there is only 1 child element the ARRAY looks like this:
Array
(
[sync] => Array
(
[learnerid] => 1234
[learnername] => Father Christmas
[password] => hoho
[clientid] => Companyname
[deviceid] => 023408fsdf8032
[lastsync] => 20051214132201
[event] => Array
(
[core] => course details
[enginename] => ultaextreme
[engine_version] => 1.0.0
[extended] => answers
)
)
)
I REALLY NEED IT TO LOOK LIKE THIS:
Array
(
[sync] => Array
(
[learnerid] => 1234
[learnername] => Father Christmas
[password] => hoho
[clientid] => Companyname
[deviceid] => 023408fsdf8032
[lastsync] => 20051214132201
[event] => Array
(
[0] => Array
(
[core] => course details
[enginename] => ultaextreme
[engine_version] => 1.0.0
[extended] => answers
)
)
How can this be done?
Thanks again for a great script…
Comment by kris — January 18, 2006 @ 4:16 pm
Hi,
Great script, It is doing exactly what I need it to do. I have a couple questions, and I have to first say I know just enough php to get me in trouble.
Im using your script in my wordpress blog and have 3 areas that the results will appear in left side bar (lside.php) the content or post section (index.php) and the right side (rside.php)
What I want to do is have results 1,2 & 3 in my left side bar, 4-9 in the center (index) and 1- 15 in the right side bar (rside.php)
To complicate the issue a variable will be place in the xml feed url that is being fetched.
I have been able to pass the var to the url xml string and everything worked - the data was passed back flawlessly.
Here is where my stupidity comes into play.
How do i take a var and echo the data on the page.
The file being returned is:
Heading, url, uri, description but keep in mind this needs to be placed in 3 different areas of the page and three files are included to actually build the page.
Any help you can give me will be greatly appreciated as I have been searching for a script that would do this for some time now.
Thanks John
Comment by John — February 9, 2006 @ 10:39 pm
Nice script
If you want to use this with PHP5 change line 25:
$result [$vals [$i][’tag’]] = array_merge ($attributes, GetChildren ($vals, $i, ‘open’));
To
$result [$vals [$i][’tag’]] = array_merge ((array)$attributes, (array)GetChildren ($vals, $i, ‘open’));
Comment by Michael G — March 2, 2006 @ 3:34 am