tags:

views:

88

answers:

4

I am trying to read an attribute value of the root node of an XML fed into the simplexml_load_string() method in PHP.

Here is an example,

<user status="Active">
</user>

I am trying to read the value of 'status' in the above XML string.

--- Here is an update ---

Below is the XML Structure,

<data status="Active">
 <user> <userid> 1 </userid> </user>
 <user> <userid> 2 </userid> </user>
<data>

The statements below dont work too.. var_dump() outputs "NULL" echo (string)$xml -> user[0] -> userid; echo (string)$xml->attributes()->status

Could some1 lead me on this ?

Regards, Immanuel

+2  A: 

echo (string)$userNode->attributes()->status should work

meder
Is the explicit cast necessary with `echo` or is it merely "good style"?
Tomalak
@Tomalak - in the simplexml implementations ive messed with, I had to explicitly do it or it'd try to echo the object reference out. perhaps in newer implementations you don't have to do it since the toString method automatically does it?
meder
A: 
$xml = simplexml_load_string('<user status="Active"></user>');

echo $xml->attributes()->status;
jakenoble
+2  A: 

Besides the attributes() method you can simply access the element's attributes by treating the element object like an array (SimpleXMLElement implements ArrayAccess)

$xml = simplexml_load_string('<user status="Active"></user>');
echo 'status=', $xml['status'];

But to access attributes in other namespaces you have to use attributes() (afaik).

VolkerK
A: 

Thanks for the replies... Parsing was not really the problem.

I missed the following cURL parameters,

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

I was treating a boolean value (cURL response) as an XML file :)

Thanks all...

Immanuel