I am trying to assign the <all>
/ <avg>
value in this XML code to a variable, so that I can use it for calculations. But when I do this, and try to print the value, I get a blank screen. Can someone please help?
<stats>
<type id="a">
<buy>
<volume>698299009</volume>
<avg>17.94</avg>
<max>18.45</max>
<min>1.00</min>
</buy>
<sell>
<volume>16375234</volume>
<avg>21.03</avg>
<max>24.99</max>
<min>20.78</min>
</sell>
<all>
<volume>714674243</volume>
<avg>18.01</avg>
<max>24.99</max>
<min>1.00</min>
</all>
</type>
</stats>
The php code I am using is as follows:
$xml = simplexml_load_file("values.xml");
$unit_value = $xml->xpath("/stats/type[@id='a']/buy/avg/")->nodeValue;
echo $unit_value;
xpath returns an array of SimpleXMLElement objects .. so you can do this :
$unit_value = $xml->xpath("//stats//type[@id='a']//buy//avg");
echo (string)$unit_value[0]; // cast to string not required
or if you are using PHP => 5.4 you can do this :
$unit_value = $xml->xpath("//stats//type[@id='a']//buy//avg")[0];
echo $unit_value;