Help needed parsing XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AndyX
Forum Newbie
Posts: 2
Joined: Mon Sep 28, 2009 6:42 am

Help needed parsing XML

Post 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!
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Help needed parsing XML

Post by Mark Baker »

$result1 = $xml->chart_data->row->number[2];
AndyX
Forum Newbie
Posts: 2
Joined: Mon Sep 28, 2009 6:42 am

Re: Help needed parsing XML

Post by AndyX »

Brilliant. Thanks Mark!
Post Reply