Amit Dhamu

Writes Code

SimpleXML

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

<?xml version="1.0" encoding="UTF-8"?>
<songs>
    <song>
        <artist><![CDATA[Coldplay]]></artist>
        <title><![CDATA[Paradise]]></title>
        <album><![CDATA[Mylo Xyloto]]></album>
    </song>
</songs>


<?php
    $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
    }
?>


simplexml / xml / php / rss

← Using RGBa Encode & Decode JSON →