Fredrik
Fredrik

Reputation: 1302

PHP XML simple_load id

How do I parse "id" from the following event

<event id="100990">
   <title>myTitle</title>
</event>

I parse the title like this:

$xml->event[0]->title

Upvotes: 0

Views: 109

Answers (3)

brian_d
brian_d

Reputation: 11385

Use the attributes method of SimpleXML

$id_attribute = (string)$xml->event[0]->attributes()->id;

Upvotes: 2

user142162
user142162

Reputation:

You access attributes just like you would access elements of an associative array:

$xml_node['id'] // The value of the attribute `id` of the node `$xml_node`

The returned value is an object (with a __toString method), so you may want to typecast the returned value.

Upvotes: 2

benhowdle89
benhowdle89

Reputation: 37464

This is exactly what you need: http://www.php.net/manual/en/simplexmlelement.attributes.php

Upvotes: 0

Related Questions