Whoooop.co.uk

XML to Array

by John on Mar.20, 2005, under PHP, XML

1111351877

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

  • Fatih

    Thanks for such a useful code… That is waht I needed…

  • Matt

    Fantastic. Fast and exactly what I was looking for. Thanks!

  • Michael

    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

  • cristi

    The script don’t work in PHP5. :(

  • CaShY

    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

  • CaShY

    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

  • cristi

    Thanks, CaShY. You help me very much.

  • ibrahim

    any idea of how to fix the problem that Michael pointed out ?

    Thanx

  • ibrahim

    with this simple xml file , i get all attributes but not the name of the city

    exemple :

    BC
    516521
    -1212891
    -800
    1

  • Jose

    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.

  • simon

    wow,wonderful~
    thx u very much,that’s what I needed

  • Watches

    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 :)

  • cid73

    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).

  • kris

    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…

  • John

    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

  • Michael G

    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’));

  • Rockstar

    Thanks for the simple n elegent script !!!

  • dave

    So where is the link to get the script??

  • phpguy

    it so helpful,thanks a ton

  • mick

    What a great script! Thanks!

2 Trackbacks / Pingbacks for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!