TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (6) Thread Tools Search this Thread Display Modes
Old 09-14-2007, 02:03 PM   6 links from elsewhere to this Post. Click to view. #1 (permalink)
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, 1392 views)
File Type: xml talkphp.xml (4.6 KB, 1796 views)

Last edited by Wildhoney : 09-17-2007 at 11:46 AM.
Karl is offline  
Reply With Quote
Old 09-17-2007, 11:54 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You can't beat SimpleXML! Can you?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-17-2007, 11:57 AM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

That looks awesome :O
Tanax is offline  
Reply With Quote
Old 09-17-2007, 01:18 PM   #4 (permalink)
The Contributor
 
Shaun's Avatar
 
Join Date: Sep 2007
Posts: 41
Thanks: 0
Shaun is on a distinguished road
Default

SimpleXML is php5 right?

Nice example on how to use it :)
Send a message via MSN to Shaun Send a message via Skype™ to Shaun
Shaun is offline  
Reply With Quote
Old 09-17-2007, 01:26 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Hmm. I'm sure that SimpleXML was available for PHP4 as an external module, but in PHP5 it became native to PHP5.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-17-2007, 01:35 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I just love how simple (pardon the pun) SimpleXML is to use. When comparing it to the nasty old days of parsing manually through an XML document, using SimpleXML is almost child's play.

PHP Code:
/*
    Challenge: 
        Output the title and author of the latest post within
        the Tips & Tricks forum on TalkPHP, in two lines of code.
        (Code can be wrapped for readability)
*/

$rss = new SimpleXMLElement(
    
'http://www.talkphp.com/external.php?lastpost=1&forumids=10',
    
LIBXML_NOCDATA,
    
true);
    
printf('<p><strong>%s</strong> by %s</p>'
    
$rss->channel->item[0]->title
    
$rss->channel->item[0]->children('http://purl.org/dc/elements/1.1/')->creator); 
The complicated children() line above is just to access a namespaced node (dc:creator) -- it's nothing scary. The LIBXML_NOCDATA flag will likely be the subject of an article/post here sooner or later.

Last edited by Salathe : 09-17-2007 at 02:14 PM.
Salathe is offline  
Reply With Quote
Old 09-17-2007, 04:40 PM   #7 (permalink)
The Visitor
 
Join Date: Sep 2007
Posts: 4
Thanks: 0
The Avangelist is on a distinguished road
Default

But what does your xml file look like?

Dont you need to install an rss reader to be able to use it as an rss feed? I have never really understood that bit, still learning XML but struggling with how exactly you set your php file to decipher the xml file.

and about 100 other really irritating questions.

I am trying to work out how to create navigation menu with xml at the moment but in asp, it's fine if you use one of the bullcrap built in controls but what about making your own! lazy microsoft.. lazy
The Avangelist is offline  
Reply With Quote
Old 09-17-2007, 05:01 PM   #8 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Hi Avangelist.

If you take a look at the attachments for the main post you'll see one file called 'talkphp.xml'. This is the file that the code uses. So if you look inside that and then take another look at the code, possibly even run it on your local machine if you can, then I'm sure things will become a little clearer :)

As for your second question, no, you do not need an RSS reader to read a RSS feed, the above code does just that, reads as RSS formatted XML file.
Karl is offline  
Reply With Quote
Old 01-15-2008, 10:00 AM   #9 (permalink)
The Visitor
 
Join Date: Sep 2007
Posts: 4
Thanks: 0
The Avangelist is on a distinguished road
Default PHP5 does it for you

Wow, that response was almost as slow as BT!

PHP5 has the incredibly simple XMLParser global which is a swizz to use so now I have no problems doing anything with XML files.

What would be good is how to write to file with one so that you can easily update the file with new data.
The Avangelist is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/general/1110-rss-parsing-simplexml.html
Posted By For Type Date
Quick Web Source - rss parsing with simplexml This thread Refback 01-14-2008 08:26 AM
Help with a XML/RSS Parser in php - SitePoint Forums This thread Refback 01-07-2008 11:43 AM
PHP RSS feed generation - SitePoint Forums This thread Refback 01-07-2008 11:42 AM
Online PHP Tutorials This thread Refback 12-28-2007 01:21 PM
PHP RSS Parsing with SimpleXML Tutorial This thread Refback 12-26-2007 10:45 AM
PHP XML and PHP RSS Parsing with SimpleXML Tutorial This thread Refback 12-23-2007 07:01 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:03 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design