1

Hello, I have this piece of XML:

<foo id="whatever" default="whatever"> FOO </foo> 

How can I access attributes like id or default?

If I print_r($xml->foo) I see them, but they are prepended with @

1 Answer 1

3

Use the array syntax for attributes and object syntax for child nodes:

$xml->foo['id'] // get `id` attribute on `foo` node 

See the Using attributes example here: http://docs.php.net/manual/en/simplexml.examples-basic.php#example-4741

See also the attributes() method.

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

2 Comments

but $node['id] gives me a object containing a array with that value, not the value :| Isn't there a quicker way to do this?
you can cheat and cast it to a string with (string)$node['id']

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.