As you have seen we can easily use DOM to manipulate XML and HTML and it's really very very simple.
You use the $node->getElementsByTagName('title')->item(0)->nodeValue , for instance the <title> tag, you can grab the title.
If you have many nodes, you do a for each loop. Say you have 3 list items in <UL>, you can declare $listitems = $doc->getElementsByTagName('li').
Now we have a group of list items, all we have to do now is loop through them and get our stuff. for each ($listitems as $listitem){ $listitem_output = $listitem->item(0)->nodeValue; echo $listitem_output;}
Isn't that great?
Feel free to checkout some posts on how to get attributes using the getAttribute property from DOM, very exciting stuff this is.
Thursday, April 21
Friday, July 17
RSS PHP: Tutorial #1
In this first RSS PHP Tutorial, we'll look at how PHP uses XML to parse the RSS file.
Here's an example of parsing a RSS file using the function below.
And there you have your own RSS parser. All we did was put the feed items into an array, now you do the rest!
Here's an example of parsing a RSS file using the function below.
$rss_tags = array(
‘title’,
‘link’,
‘guid’,
‘comments’,
‘description’,
‘pubDate’,
‘category’,
);
$rss_item_tag = ‘item’;
$rss_url = ‘http://rssphp.blogspot.com/feeds/posts/default’;
$rssfeed = rss_to_array($rss_item_tag,$rss_tags,$rss_url);
print_r($rssfeed);
function rss_to_array($tag, $array, $url) {
$doc = new DOMdocument();
$doc->load($url);
$rss_array = array();
$items = array();
foreach($doc->getElementsByTagName($tag) AS $node) {
foreach($array AS $key => $value) {
$items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;
}
array_push($rss_array, $items);
}
return $rss_array;
}
?>
And there you have your own RSS parser. All we did was put the feed items into an array, now you do the rest!
Labels:
PHP Functions
Thursday, July 16
RSS PHP Parsers: Your own RSS Aggregator
RSS PHP Parsers basically allows you to use PHP and the power of RSS to create your own Aggregator. Meaning you can now have an automated website or blog with RSS feeding into it, essentially your own RSS Aggregator.
RSS Parsers make it simple to use the RSS feeds as they deal with RSS parsing issues.
Here we have collected the best free RSS to PHP parsers available.
MagpieRSS
Easy to use RSS PHP parser.
RSS 1.0, 2.0, Atom.
SimplePie
SimplePie is a library of PHP code which parses RSS feeds.
lastRSS
A powerful PHP class for parsing RSS feeds.
All RSS versions supported.
Zend_Feed
Another easy to use RSS parser.
RSS Parsers make it simple to use the RSS feeds as they deal with RSS parsing issues.
Here we have collected the best free RSS to PHP parsers available.
MagpieRSS
Easy to use RSS PHP parser.
RSS 1.0, 2.0, Atom.
SimplePie
SimplePie is a library of PHP code which parses RSS feeds.
lastRSS
A powerful PHP class for parsing RSS feeds.
All RSS versions supported.
Zend_Feed
Another easy to use RSS parser.
Subscribe to:
Posts (Atom)