Forum Moderators: coopster
I just upgraded to php5 and I'm trying to access an attribute from an xml file.
<?php
$xml = simplexml_load_file('data-1170371187.xml');
print_r($xml);
print "<hr>";
print $xml->results[r][0][q]; //doesn't work!foreach ($xml->results->r as $results) {
print_r($results);//->attributes);//['q'];
print $results->attributes['q'];
print '<hr>';
?>
Here is the XML
<?xml version="1.0"?>
<resultset>
<results compact="true" maximum="15" offset="0" query="lamp">
<r m="718049" q="projector lamp" t="1034557"/>
<r m="457750" q="table lamp" t="659521"/>
<r m="372177" q="floor lamp" t="536229"/>
<r m="338905" q="lamp" t="488291"/>
<r m="186251" q="lamp shades" t="268348"/>
<r m="126039" q="fluorescent lamp" t="181595"/>
<r m="103001" q="tiffany lamp" t="148403"/>
<r m="93551" q="lcd projector lamp" t="134787"/>
<r m="45679" q="lamp shade" t="65814"/>
<r m="41587" q="desk lamp" t="59918"/>
<r m="36952" q="oil lamp" t="53240"/>
<r m="34568" q="lava lamp" t="49805"/>
<r m="18893" q="tanning lamp" t="27221"/>
<r m="16174" q="antler lamp" t="23304"/>
<r m="15530" q="halogen floor lamp" t="22376"/>
</results>
</resultset>
I need to access the 'q' attribute in each of the 'r' elements.
No matter what I try, it doesn't seem to work...
For future reference (if you haven't already visited) php.net's Documentation on SimpleXML [us2.php.net] is very nice with plenty of examples.