0
SimpleXMLElement Object ( [0] => CEM ) 

There is a SimpleXMLElement Object like this. I tried accessing it with $object->0 and $object->{0}. But it is giving a php error. How do we access it with out changing it to another format.

1
  • Does $object[0] work? Commented Apr 5, 2013 at 7:47

2 Answers 2

1

From your print_r output it might not be really obvious:

SimpleXMLElement Object ( [0] => CEM ) 

This is just a single XML element containing the string CEM, for example:

<xml>CEM</xml> 

You obtain that value by casting to string (see Basic SimpleXML usageDocs):

(string) $object; 

When you have a SimpleXMLElement object and you're unsure what it represents, it's easier to use echo $object->asXML(); to output the XML and analyze it than using print_r alone because these elements have a lot of magic that you need to know about to read the print_r output properly.

An example from above:

<?php $object = new SimpleXMLElement('<xml>CEM</xml>'); print_r($object); echo "\n", $object->asXML(), "\n"; echo 'Content: ', $object, "\n"; // echo does cast to string automatically 

Output:

SimpleXMLElement Object ( [0] => CEM ) <?xml version="1.0"?> <xml>CEM</xml> Content: CEM 

(Online Demo)

Sign up to request clarification or add additional context in comments.

4 Comments

Inevitable plug for my simplexml_dump and simplexml_tree functions. Not 100% perfect, but miles better than what print_r gives you. github.com/imsop/simplexml_debug
Oh nice, I have dome this but it's for DOMDocument: Debug a DOMDocument Object in PHP. It works with iterators so it probably can be easily adoped to SimpelXMLElement specificaly as SimpleXMLIterator which implements RecursiveIterator as I (just) see. Never tried to port that on simplexml but it should work. If you like simpexml and as I quickly scanned your code, you might be interested as well in this: SimpleXML Type Cheatsheet - it's a bit special interest but can be handy.
Ooh, I'd not thought of using == to check for the different types like that, I shall have to use that :)
Use wherever you see fit. AFAIK this was not documented, I have placed it as an answer here on site as well for backup: How to tell apart SimpleXML objects representing element and attribute?
0

In your case, it appears as you do print_r($object) and $object it's an array so what you are viewing is not an XML objet but an array enclosed in the display object of print_r()

any way, to access a simpleXML object you can use the {} sytax here it goes:

$xmlString = '<uploads token="vwl3u75llktsdzi"> <attachments> <attachment myattr="attribute value">123456789</attachment> </attachments> </uploads>'; $xml = simplexml_load_string($xmlString); echo "attachment attribute: " . $xml->{"attachments"}->attributes()["myattr"] " \n"; echo " uploads attribute: " . $xml->{"uploads"}->attributes()["token"] . "\n"; 

you can replace "attachments" with $myVar or something

remember attributes() returns an associative array so you can access data with square braces or through array_keys() or do a foreach cycle.

In your specific case may be just

echo $object[0]; // returns string "CEM" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.