Accessing a SimpleXMLElement object

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
juanfhj
Forum Newbie
Posts: 2
Joined: Sat Dec 19, 2009 8:58 am

Accessing a SimpleXMLElement object

Post by juanfhj »

I have the following SimpleXMLElement:

Code: Select all

 
SimpleXMLElement Object
(
    [connection] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [ip] => 200.12.186.6
                    [port] => 1234
                )
            [1] => SimpleXMLElement Object
                (
                    [ip] => 200.12.186.7
                    [port] => 1236
                )
        )
)
In $xml. How can I access ip directly? I can only get it wrapped in the SingleXMLElement object. If I try $xml->connections->connection[1]->ip, I get

Code: Select all

 
SimpleXMLElement Object
(
    [0] => 200.12.186.7
)
And if I access $xml->connections->connection[1]->ip[0], I still get

Code: Select all

 
SimpleXMLElement Object
(
    [0] => 200.12.186.7
)
 
What's wrong here? My XML looks like this:

Code: Select all

<?xml version='1.0' standalone='yes'?>
<stuff>
<connections>
<connection><ip>200.12.186.6</ip><port>1234</port></connection>
<connection><ip>200.12.186.7</ip><port>1236</port></connection>
</connections>
</stuff>
Thanks
juanfhj
Forum Newbie
Posts: 2
Joined: Sat Dec 19, 2009 8:58 am

Re: Accessing a SimpleXMLElement object

Post by juanfhj »

Ok, it's an intended behavior. The element should be accessed like

Code: Select all

(string)$xml->connections->connection[1]->ip
It was listed as a bug in 2004, but it's actually by design.
http://bugs.php.net/bug.php?id=29500&edit=1
The bug report doesn't address why the behavior is intended though. Could anybody tell me?
Post Reply