Extract value from XML using PHP

I want to get the value of “passed” under Course/CoreData/Status and assign it to a PHP variable. Here’s my data stored in a mysql database field:


<Course>
  <LearnerID value="tim@mail.net"/>
  <Result>
    <CoreData>
      <Status value="passed"/>
      <Location value="1"/>
        </CoreData>
  </Result>
</Course>

I’ve tried the following code as a start but to no avail:


$sql = "SELECT * FROM exam";
$q	 = mysql_query($sql) or die(mysql_error());

while($row=mysql_fetch_array($q)){
	$Filedata=$row["Filedata"];
}

$xml = simplexml_load_string($Filedata);

Please help put an end to my two days of misery trying to figure this out :slight_smile:

Thanks,

Tim

<?php
// Suggest the XML needs to be written in long form (not using attributes like <Status value=“passed”/>).
// And the XML needs to be retrieved as a string:

$response = <<<XML
<Result>
<LearnerID>tim@mail.net</LearnerID>
<CoreData>
<Status>passed</Status>
<Location>1</Location>
</CoreData>
</Result>
XML;

$xml = simplexml_load_string($response);

// print_r($xml);

  foreach ($xml-&gt;CoreData as $coredata) {
    echo 'Status: '.$coredata-&gt;Status.'; Location: '. $coredata-&gt;Location;
  }

?>


$Filedata = '<Course> 
  <LearnerID value="tim@mail.net"/> 
  <Result> 
    <CoreData> 
      <Status value="passed"/> 
      <Location value="1"/> 
        </CoreData> 
  </Result> 
</Course>';

$xml = simplexml_load_string($Filedata);
$status = (string) $xml->Result->CoreData->Status['value'];

echo $status; // passed

See http://php.net/simplexml.examples-basic