Page 1 of 1

Help needed parsing XML

Posted: Mon Sep 28, 2009 6:52 am
by AndyX
Hi, I'm hoping someone may be able to help...


I'm trying to display a single value from an XML file but am getting a blank output (but no errors).

The code I'm using is this:

Code: Select all

if(!$xml=simplexml_load_file('sample.xml')){
        trigger_error('Error reading XML file',E_USER_ERROR);
}
$result = $xml->test; //This is just a test which works
$result1 = $xml->number[2];
echo $result; // This displays TEST
echo $result1; //I was hoping this would display 3000
And this is the XML:

Code: Select all

 <test>TEST</test>
  <chart_data>
    <row>
      <string></string>
      <number tooltip="£3000">3000</number>
      <number tooltip="£3000.56">3000.56</number>
      <number tooltip="£3000">3000</number>
    </row>
  </chart_data>
Any help would be really appreciated!

Re: Help needed parsing XML

Posted: Mon Sep 28, 2009 7:48 am
by Mark Baker
$result1 = $xml->chart_data->row->number[2];

Re: Help needed parsing XML

Posted: Mon Sep 28, 2009 10:13 am
by AndyX
Brilliant. Thanks Mark!