XML to Array
by John on Mar.20, 2005, under PHP, XML
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.
Related Posts
20 Comments for this entry
2 Trackbacks / Pingbacks for this entry
-
Taste of Tech » Blog Archive » SaveLinks
March 11th, 2009 on 6:53 pm[...] script is here, if you’re interested. I should also mention that it uses this script to parse the XML data. I can’t guarantee that it’ll be of any use to anyone else, but [...]
-
Taste of Tech | SaveLinks
March 13th, 2009 on 7:37 pm[...] script is here, if you’re interested. I should also mention that it uses this script to parse the XML data. I can’t guarantee that it’ll be of any use to anyone else, but [...]
March 23rd, 2005 on 5:29 pm
Thanks for such a useful code… That is waht I needed…
March 28th, 2005 on 10:49 pm
Fantastic. Fast and exactly what I was looking for. Thanks!
March 31st, 2005 on 3:15 am
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
March 31st, 2005 on 6:07 pm
The script don’t work in PHP5.
March 31st, 2005 on 6:16 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
March 31st, 2005 on 6:20 pm
basically
SNIP]
function GetXMLTree ($xmldata)
{
$attributes =array();
// we want to know if an error occurs
ini_set (‘track_errors’, ’1′);
[SNIP]
makes it work in php5
April 2nd, 2005 on 8:47 am
Thanks, CaShY. You help me very much.
April 19th, 2005 on 10:40 am
any idea of how to fix the problem that Michael pointed out ?
Thanx
April 19th, 2005 on 10:41 am
with this simple xml file , i get all attributes but not the name of the city
exemple :
BC
516521
-1212891
-800
1
April 22nd, 2005 on 10:02 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']] = array_merge ($attributes, GetChildren ($vals, $i, ‘open’));
—– fixed ——–
$result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, $vals[$i]['type']));
Sorry for my poor english and thanks for the code.
May 20th, 2005 on 7:59 pm
wow,wonderful~
thx u very much,that’s what I needed
May 23rd, 2005 on 11:44 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
May 26th, 2005 on 11:09 am
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
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1)
into
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 0)
and it can’t work.
pls fix this problem, I’ll be still waiting. Thanx again(and sorry for my poor English).
January 18th, 2006 on 4:16 pm
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
(
[learner_id] => 1234
[learner_name] => Father Christmas
[password] => hoho
[client_id] => Companyname
[device_id] => 023408fsdf8032
[last_sync] => 20051214132201
[event] => Array
(
[core] => course details
[engine_name] => ultaextreme
[engine_version] => 1.0.0
[extended] => answers
)
)
)
—————————
I REALLY NEED IT TO LOOK LIKE THIS:
—————————
Array
(
[sync] => Array
(
[learner_id] => 1234
[learner_name] => Father Christmas
[password] => hoho
[client_id] => Companyname
[device_id] => 023408fsdf8032
[last_sync] => 20051214132201
[event] => Array
(
[0] => Array
(
[core] => course details
[engine_name] => ultaextreme
[engine_version] => 1.0.0
[extended] => answers
)
)
)
)
—————————
How can this be done?
Thanks again for a great script…
February 9th, 2006 on 10:39 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
March 2nd, 2006 on 3:34 am
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’));
July 10th, 2009 on 10:06 am
Thanks for the simple n elegent script !!!
August 7th, 2009 on 1:18 pm
So where is the link to get the script??
September 29th, 2009 on 5:28 pm
it so helpful,thanks a ton
December 23rd, 2009 on 10:11 pm
What a great script! Thanks!