Parsing XML with PHP


Parsing XML using PHP is pretty easy. PHP has an extension called SimpleXML that "provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators." Check out the example below to see how it works. The PHP code is first with the sample XML below it.

    $xml = simplexml_load_file("");

    $items = $xml->channel->item;

    for($i = 0; $i < count($items); $i++) { 
        $title = $items[$i]->title;
        $url = $items[$i]->link;
        echo "<a href='" . $url . "'>" . $title . "</a>";

<rss version="2.0">
        <title>Hacker News</title>
        <description>Links for the intellectually curious, ranked by readers.</description>
            <title>Sphinxtr: Creating a Portable PhD Thesis</title>
                <![CDATA[<a href="">Comments</a>]]>