Tuesday, July 16, 2013

Reading Xml in PHP using SimpleXML

suppose you have an xml file "sample.xml" on your server with the following content:

<samples>
<sample index="1" value="0.3" />
<sample index="2" value="0.332" />
<sample index="3" value="0.83" />
<sample index="4" value="0.23" />
</samples>

The following codes will transform the above xml file into a html table using SimpleXML:

<?php
echo "<table>";
$filepath="sample.xml";
$xml=simplexml_load_file($filepath);
foreach($xml->children() as $child)
{
$index=$child->attributes()->index;
        $val=$child->attributes()->value;
        echo "<tr><td>".$index."</td><td>".$val."</td></tr>";
}
echo "</table>";
?>

No comments:

Post a Comment