XML - Listing Children Nodes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Scrutters
Forum Newbie
Posts: 3
Joined: Tue Dec 01, 2009 4:48 am

XML - Listing Children Nodes

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: XML - Listing Children Nodes

Post by yacahuma »

did you try simplexml already?
Scrutters
Forum Newbie
Posts: 3
Joined: Tue Dec 01, 2009 4:48 am

Re: XML - Listing Children Nodes

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: XML - Listing Children Nodes

Post 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.
Scrutters
Forum Newbie
Posts: 3
Joined: Tue Dec 01, 2009 4:48 am

Re: XML - Listing Children Nodes

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: XML - Listing Children Nodes

Post by yacahuma »

you may find this link useful IF caching turns out to be your solution
http://articles.sitepoint.com/article/c ... rformance#
Post Reply