d
Amit DhamuSoftware Engineer
 

SimpleXML

1 minute read 00000 views

Learn how to use SimpleXML to parse XML documents using PHP.

<songs>
    <song>
        <artist>Coldplay></artist>
        <title>Paradise></title>
        <album>Mylo Xyloto></album>
    </song>
</songs>
$xml_file = file_get_contents('songs.xml');
$xml = new SimpleXMLElement($xml_file);
$songs = $xml->song;

foreach ($songs as $song) {
    echo $song->artist // returns Coldplay
    echo $song->title // returns Paradise
    echo $song->album // returns Mylo Xyloto
}