View Single Post
Old 09-14-2007, 03:03 PM   #1 (permalink)
Karl
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default RSS Parsing with SimpleXML

This tutorial will provide a simple example of one method for parsing an XML document, more specifically, an RSS formatted XML document. This tutorial will use the new Simple XML object added to PHP 5.

If you wish to follow this tutorial properly then you should first download the RSS XML document attached to this thread called talkphp.xml. Now you’ve done that, create a new file on your website and add the following code:

PHP Code:
<?php

// Load the XML data from the specified file name.  
// The second argument (NULL) allows us to specify additional libxml parameters,
// we don't need this so we'll leave it as NULL.  The third argument however is
// important as it informs simplexml to handle the first parameter as a file name
// rather than a XML formatted string.
$pFile = new SimpleXMLElement('talkphp.xml'nulltrue);  

// Now that we've loaded our XML file we can begin to parse it.
// We know that a channel element should be available within the,
// document so we begin by looping through each channel
foreach ($pFile->channel as $pChild)
{    
    
// Print our channel specific information, this should be
    // easy to understand, basically we're grabbing the 
    // title, descripting and link nodes and outputting their values
    
echo "<h1>" $pChild->title "</h1>\n";
    echo 
"<p>\n";
    echo 
$pChild->description "<br />\n";
    
printf('Visit us at <a href="%s">%s</a><br />' "\n",  $pChild->link$pChild->link);
    echo 
"</p>\n";
    
    
// Now we want to loop through the items inside this channel
    
foreach ($pFile->channel->item as $pItem)
    {
        echo 
"<p>\n";
        
        
// If this item has child nodes as it should, 
        // loop through them and print out the data
        
foreach ($pItem->children() as $pChild)
        {
            
// We can check the name of this node using the getName() method.
            // We can then use this information, to, for example, embolden
            // the title or format a link
            
switch ($pChild->getName())
            {
                case 
'title':
                    echo 
"<b>$pChild</b><br />\n";
                    break;
                    
                case 
'link':
                    
printf('<a href="%s>%s</a><br />' "\n"$pChild$pChild);
                    break;
                    
                default:
                    echo 
nl2br($pChild) . "<br />\n";
                    break;
            }
        }
        
        echo 
"</p>\n";
    }
}

?>
In order to run this code you must name the XML document ‘talkphp.xml’ and place it in the same directory as PHP file. Please note, for the sake of simplicity I've removed all error checking and validation.

That’s all there is to using SimpleXML, easy, huh?
Attached Files
File Type: php smartxml_example.php (1.8 KB, 1192 views)
File Type: xml talkphp.xml (4.6 KB, 1543 views)

Last edited by Wildhoney : 09-17-2007 at 12:46 PM.
Karl is offline  
Reply With Quote