Page 1 of 1

XML - Listing Children Nodes

Posted: Tue Dec 01, 2009 4:54 am
by Scrutters
Hi,

I need something list all the nodes of an XML file in a similar way to how the print_r() function lists out an array.
So it will list out all the fields in the correct order but so that I can actually do something with them.

Code: Select all

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
    [d] => Array
        (
            [0] => x
            [1] => y
            [2] => Array
                 (
                     [0] => Keeps going deeper...
                     [1] => etc...
                 )
        )
)
I'm trying to create a select box with all the fields in so that I can map the XML file into my database.
At the moment I am doing this:

Code: Select all

foreach($xml->children() as $child1){
        foreach($child1->children() as $child2){
                 foreach($child2->children() as $child3){
                         // Obviously this could keep going for ages...
                 }
        }
}
Really need something that can test to see if there are any children nodes under the current node selected and keep going till it stops then move on the the next parent node.
Or something that can count all nodes within the feed and just list them one by one.

I'm sure there must be a way to do this, I just can't work out the logic.

Re: XML - Listing Children Nodes

Posted: Tue Dec 01, 2009 8:27 pm
by yacahuma
did you try simplexml already?

Re: XML - Listing Children Nodes

Posted: Wed Dec 02, 2009 2:09 am
by Scrutters
Yeah I've been using SimpleXML, luckily I have actually found a solution now.
Although now because I'm loading this list multiple times on a page, my server is starting to struggle so need to look into converting the feed into an array on page load.

I think I have a way to do that so will have a look later on.

Re: XML - Listing Children Nodes

Posted: Wed Dec 02, 2009 2:14 am
by John Cartwright
Scrutters wrote:Yeah I've been using SimpleXML, luckily I have actually found a solution now.
Although now because I'm loading this list multiple times on a page, my server is starting to struggle so need to look into converting the feed into an array on page load.

I think I have a way to do that so will have a look later on.
A common solution to avoiding making an external request is to cache the XML document with a set expiration to determine when to acquire an updated copy, and of course you would serve the locally cached version.

Re: XML - Listing Children Nodes

Posted: Wed Dec 02, 2009 2:52 am
by Scrutters
I was under the impression PHP didn't have a cacheing system?
I've looked into it in the past as it would be quite useful, for other things as well as this.

Re: XML - Listing Children Nodes

Posted: Wed Dec 02, 2009 4:42 am
by yacahuma
you may find this link useful IF caching turns out to be your solution
http://articles.sitepoint.com/article/c ... rformance#