Submitted by chrisst1 on Wed, 2008-04-02 12:17 in Magic Parser
Hi David,
I have looked through the forum is there any way using Magic Parser to extract items from a large XML/CSV feed and output into a new XML/CSV feed based upon categories in the original feed?
It's relatively easy to do this, but it does involve creating the entire new feed as your output from the script; deciding whether or not to output each record based on a value (or values) in the source feed.
As an example, here is the code that would re-construct the BBC News RSS feed, but only for items in the World category:
<?php
require("MagicParser.php");
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss>";
print "<channel>";
function myRecordHandler($record)
{
if ($record["CATEGORY"] == "World")
{
// create your new XML output here - always use CDATA tags to make sure content is safe
print "<item>";
print "<title><![CDATA[".$record["TITLE"]."]]></title>";
print "<description><![CDATA[".$record["DESCRIPTION"]."]]></description>";
print "<link><![CDATA[".$record["LINK"]."]]></link>";
print "<category><![CDATA[".$record["CATEGORY"]."]]></category>";
print "</item>";
}
}
MagicParser_parse("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
print "</channel>";
print "</rss>";
exit();
?>
Hi Tasha,
It's relatively easy to do this, but it does involve creating the entire new feed as your output from the script; deciding whether or not to output each record based on a value (or values) in the source feed.
As an example, here is the code that would re-construct the BBC News RSS feed, but only for items in the World category:
<?php
require("MagicParser.php");
header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>";
print "<rss>";
print "<channel>";
function myRecordHandler($record)
{
if ($record["CATEGORY"] == "World")
{
// create your new XML output here - always use CDATA tags to make sure content is safe
print "<item>";
print "<title><![CDATA[".$record["TITLE"]."]]></title>";
print "<description><![CDATA[".$record["DESCRIPTION"]."]]></description>";
print "<link><![CDATA[".$record["LINK"]."]]></link>";
print "<category><![CDATA[".$record["CATEGORY"]."]]></category>";
print "</item>";
}
}
MagicParser_parse("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
print "</channel>";
print "</rss>";
exit();
?>
Hope this points you in the right direction!
Cheers,
David.