hi all
is it possible to merge (2 rss feeds) or (2 xml files) or (2 parsed magicparser feeds) ?
Thanks for helping out
it worked but it is viewing the first source item then the second source
but i want to mix the result of the new source
is that possible ?
Hello fraizor,
Sure - the trick is to parse each feed into a global array, with common field names for each of the fields. Then, after parsing all feeds, shuffle() the array and then output as you were before, basically using the same record handler function, but being called from a foreach() loop instead of the parser. Consider the following (based on your previous code)
<?php
require("MagicParser.php");
$myRecords = array();
function myRSSRecordHandler($record)
{
global $myRecords;
$myRecords["TITLE"] = $record["TITLE"];
}
function myOtherRecordHandler($record)
{
global $myRecords;
$myRecords["TITLE"] = $record["NAME"];
}
$url = "http://www.example.com/rss.xml";
MagicParser_parse($url,"myRSSRecordHandler","xml|RSS/CHANNEL/ITEM/");
$url = "http://www.example.org/rss.xml";
MagicParser_parse($url,"myRSSRecordHandler","xml|RSS/CHANNEL/ITEM/");
$url = "http://www.example.net/other.xml";
MagicParser_parse($url,"myOtherRecordHandler","xml|OTHER/FORMAT/");
$txt = "";
function myDisplayRecordHandler($record)
{
global $txt;
if (isset($record["TITLE"])) {
$txt .= $txt . $record["TITLE"]."|;|";
} else {
$txt .= $txt . "False"."|;|" ;
}
}
shuffle($myRecords);
foreach($myRecords as $record)
{
myDisplayRecordHandler($record);
}
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
echo $txt;
?>
Hope this helps!
Cheers,
David
--
MagicParser.com
Hello fraizor,
Sure - basically, you just need one record handler function per feed format (so if you plan to parse multiple RSS feeds, they can be handled with a single record handler function), and this could be combined with another record handler function for a different format feed. Continuing your method of constructing a global variable $txt for subsequent output to a file from your previous post (here), consider something like;
<?php
require("MagicParser.php");
$txt = "";
function myRSSRecordHandler($record)
{
if (isset($record["TITLE"])) {
$txt = $txt . $record["TITLE"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
// plus other fields as required
}
function myOtherRecordHandler($record)
{
if (isset($record["NAME"])) {
$txt = $txt . $record["NAME"]."|;|";
} else {
$txt = $txt . "False"."|;|" ;
}
// plus other fields as required
}
$url = "http://www.example.com/rss.xml";
MagicParser_parse($url,"myRSSRecordHandler","xml|RSS/CHANNEL/ITEM/");
$url = "http://www.example.org/rss.xml";
MagicParser_parse($url,"myRSSRecordHandler","xml|RSS/CHANNEL/ITEM/");
$url = "http://www.example.net/other.xml";
MagicParser_parse($url,"myOtherRecordHandler","xml|OTHER/FORMAT/");
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
echo $txt;
?>
Hope this helps!
Cheers,
David
--
MagicParser.com