Re: cannot access SimpleXML object property
| From: | Jonathan Tapicer | Date: | Thu, 07 Jan 2010 22:31:54 +0000 |
| Subject: | Re: cannot access SimpleXML object property | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-300891@lists.php.net to get a copy of this message | ||
On Thu, Jan 7, 2010 at 6:56 PM, Mari Masuda <mari.masuda@stanford.edu> wrote:
> Hi,
>
> I am working with an XML document and have a SimpleXML object whose var_dump looks like this:
>
> ---
> object(SimpleXMLElement)#2 (10) {
> ["@attributes"]=>
> array(1) {
> ["id"]=>
> string(7) "3854857"
> }
> ["type"]=>
> string(7) "Article"
> ["createDate"]=>
> string(25) "2006-09-06T16:42:20-07:00"
> ["editDate"]=>
> string(25) "2007-07-16T09:15:53-07:00"
> ["creator"]=>
> string(19) "Michael Gottfredson"
> ["status"]=>
> string(5) "ready"
> ["field"]=>
> ...snip a bunch of stuff...
> }
> ---
>
> Assuming the above object is referenced by $current_object, I can access the values of most
> stuff with $current_object->creator or whatever. However, I cannot figure out how to access the
> value of id. I tried the following, none of which worked for me:
>
> 1. $current_object->@attributes->id (gives Parse error: syntax error, unexpected
> '@', expecting T_STRING or T_VARIABLE or '{' or '$' in
> /Applications/apache/htdocs/test.php on line 33)
>
> 2. $current_object->'@attributes'->id (gives Parse error: syntax error,
> unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or
> '$' in/Applications/apache/htdocs/test.php on line 33)
>
> 3. $current_object->{@attributes}->id (no error but is null)
>
> 4. $current_object->{'@attributes'}->id (no error but is also null)
>
> Does anyone know how I can reference the value of id? Thanks!
>
> Mari
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
$id = (string)$current_object['id'];