I'm not sure if you're tripping up on the output of print_r
, but the underlying XML for that example would be something like <foo OK="No"><NEWS>Blocked</NEWS></foo>
, so OK is an ordinary XML attribute.
As the manual's basic usage examples show, the simplest way to access an attribute with SimpleXML is using ['attribute_name']
. So in your case, it will be like this:
echo $node['OK'];
Note that like all accessors, that returns another SimpleXML object, not a string, so if you're passing it around to other parts of your code, you should cast it to a string with the syntax (string)
to avoid tripping up other code:
$ok_value = (string)$node['OK'];
As others have pointed out, there's also an attributes()
method, which lets you loop through all attributes of a node, or access attributes which are in a particular XML namespace. These two lines are equivalent to the two above:
echo $node->attributes()->OK;
$ok_value = (string)$node->attributes()->OK;