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,
<data status="Active">
<user> <userid> 1 </userid> </user>
<user> <userid> 2 </userid> </user>
<data>
I am trying to read the value of 'status' in the above XML string:
echo $xml->user[0]->userid;
echo $xml->attributes()->开发者_如何学Pythonstatus
The statements don't work too, var_dump()
outputs "NULL".
echo (string)$userNode->attributes()->status
should work
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).
$xml = simplexml_load_string('<user status="Active"></user>');
echo $xml->attributes()->status;
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...
Sorry but in your example XML you did not close your end <data>
tag.
精彩评论