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.

hakre
  • 193,403
  • 52
  • 435
  • 836
prasadmsvs
  • 1,621
  • 4
  • 18
  • 31

2 Answers2

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)

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Inevitable plug for my `simplexml_dump` and `simplexml_tree` functions. Not 100% perfect, but miles better than what `print_r` gives you. https://github.com/imsop/simplexml_debug – IMSoP Apr 07 '13 at 22:49
  • Oh nice, I have dome this but it's for DOMDocument: [Debug a DOMDocument Object in PHP](http://stackoverflow.com/a/8631974/367456). 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](http://hakre.wordpress.com/2013/02/12/simplexml-type-cheatsheet/) - it's a bit special interest but can be handy. – hakre Apr 07 '13 at 23:05
  • Ooh, I'd not thought of using `==` to check for the different types like that, I shall have to use that :) – IMSoP Apr 08 '13 at 08:15
  • 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?](http://stackoverflow.com/a/14829309/367456) – hakre Apr 08 '13 at 08:35
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"
GCoro
  • 71
  • 1
  • 2