You are here:  » Merging 2 rss feeds or 2 xml files or 2 parsed magicparser feeds


Merging 2 rss feeds or 2 xml files or 2 parsed magicparser feeds

Submitted by fraizor on Mon, 2016-08-08 22:23 in

hi all
is it possible to merge (2 rss feeds) or (2 xml files) or (2 parsed magicparser feeds) ?

Submitted by support on Tue, 2016-08-09 08:44

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

Submitted by fraizor on Tue, 2016-08-09 20:35

Thanks a lot , i think this would work

Submitted by fraizor on Wed, 2016-08-10 12:00

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 ?

Submitted by support on Wed, 2016-08-10 12:25

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