Access to some properties of the SimpleXMLElement object

When I print_r() the SimpleXMLElement object referenced by the $xmlObject variable, I see the following structure:

 SimpleXMLElement Object ( [@attributes] => Array ( [uri] => /example ) [result] => SimpleXMLElement Object ( [message] => Record(s) added successfully [recorddetail] => Array ( [0] => SimpleXMLElement Object ... ) ) ) 

Notice how the property $xmlObject->result->message looks like, it's just a string. However, if I do print_r($xmlObject->result->message) , I get the following:

 SimpleXMLElement Object ( [0] => Record(s) added successfully ) 

So at this moment I am confused. Why is $xmlObject->result->message identified as an instance of SimpleXMLElement Object in this case, when the print result of the full $xmlObject does not imply this?

And how do I access this value? I tried $xmlObject->result->message[0] , but it just prints the same thing (i.e. the last piece of code that I posted).

+7
source share
1 answer

The representation you presented when using print_r or var_dump on SimpleXMLElement has very little to do with how it is structured internally. For example, there is no @attributes property that you could access with $element['@attributes']['uri'] . You just do $element['uri']

It is as it is. SimpleXMLElement objects behave differently. Before using SimpleXml, read the examples in the PHP Manual:

To understand the implementation in detail, you need to look at the source code:

To print $xmlObject->result->message , just do echo $xmlObject->result->message . This will autosave the SimpleXMLElement line.

+7
source

All Articles