You are here:  » Parsing a live XML feed


Parsing a live XML feed

Submitted by rcasey on Wed, 2013-08-28 21:26 in

Hey David,

I'm hoping to use MagicParser to parse a live XML feed. I've got everything set up pretty well, and it is parsing OK, but things are taking between 5-10 minutes to update. I'd like them to update more often than that. Is there as a step I'm missing?

Here is some code I'm working with:

<table width="auto" class="leaders">
         <thead>
<tr>
<th>Home</th>
                <th>Score</th>
                <th>Away</th>
                <th>Status</th>
</tr>
</thead>
<tbody>
<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    // Call records:?>
             <tr>
                        <td><?php print $record["GAME-HOMESCHOOLNAME"]; ?><?php if($record["GAME-HOMESCHOOLSTATE"] == "CO") { ?><?php } else { ?> (<?php print $record["GAME-HOMESCHOOLSTATE"]; ?>)<?php ?></td>
                        <td><?php print $record["GAME-CURRENTHOMESCORE"]; ?> - <?php print $record["GAME-CURRENTAWAYSCORE"]; ?></td>
                        <td><?php print $record["GAME-AWAYSCHOOLNAME"]; ?><?php if($record["GAME-AWAYSCHOOLSTATE"] == "CO") { ?><?php } else { ?> (<?php print $record["GAME-AWAYSCHOOLSTATE"]; ?>)<?php ?></td>
                       <td class="info"><?php $search = array('F''HT''OT''Q1''Q2''Q3''Q4'); $replace = array ('Final''Half''OT''1st''2nd''3rd''4th'); echo str_replace($search$replace$record["GAME-GAMEPERIOD"]); ?></td>
                    </tr>
<?php
  }
  MagicParser_parse("url","myRecordHandler","xml|GAMES/GAME/");
?>
</tbody>
</table>

If it helps, this is a live scoreboard. I do have the page set to refresh every 120 seconds.

Submitted by support on Thu, 2013-08-29 07:59

Hi,

Do you mean the page is taking 5-10 minutes to render? That shouldn't be the case
if the file is relatively small but very large XML sources could feasibly take
that sort of time...

Would you be able to post the actual URL you are using (I'll remove before
publishing your reply) and I'll check it out for you?

Cheers,
David
--
MagicParser.com

Submitted by rcasey on Sat, 2014-01-04 04:42

David,

Thank you for your help. I've added a bit of code to this to try and make it so I don't have to update the code on a daily basis, and am having it look up today's date and returning a specific record.

I believe my PHP is correct, but for some reason, it won't return what I'm looking for. Any ideas? Here's the code:

<table width="auto" class="leaders">
         <thead>
<tr>
<th>Home</th>
                <th>Score</th>
                <th>Away</th>
                <th>Status</th>
</tr>
</thead>
<tbody>
<?php require("MagicParser.php"); ?>
 <?php function myRecordHandler($record) { ?>
 <?php if (strpos($record["GAME-LASTMODIFIED"],(date("Y-m-d"))) !==false) { ?>
             <tr>
                        <td><?php print $record["GAME-HOMESCHOOLNAME"]; ?><?php if($record["GAME-HOMESCHOOLSTATE"] == "CO") { ?><?php } else { ?> (<?php print $record["GAME-HOMESCHOOLSTATE"]; ?>)<?php ?></td>
                        <td><?php print $record["GAME-CURRENTHOMESCORE"]; ?> - <?php print $record["GAME-CURRENTAWAYSCORE"]; ?></td>
                        <td><?php print $record["GAME-AWAYSCHOOLNAME"]; ?><?php if($record["GAME-AWAYSCHOOLSTATE"] == "CO") { ?><?php } else { ?> (<?php print $record["GAME-AWAYSCHOOLSTATE"]; ?>)<?php ?></td>
                       <td class="info"><?php $search = array('F''HT''OT''Q1''Q2''Q3''Q4'); $replace = array ('Final''Half''OT''1st''2nd''3rd''4th'); echo str_replace($search$replace$record["GAME-GAMEPERIOD"]); ?></td>
                    </tr>
<?php
  }
 }
  MagicParser_parse("url","myRecordHandler","xml|GAMES/GAME/");
?>
</tbody>
</table>

I can provide you with the url of the feed, but would prefer you edit it out, if possible.

Submitted by support on Sat, 2014-01-04 08:38

Hi,

Sure - if you could let me know the URL (I'll remove before publishing your reply) I'll check it out for you!

Cheers,
David
--
MagicParser.com

Submitted by rcasey on Sat, 2014-01-04 16:00

Certainly.

Here it is: {link saved}

Submitted by support on Sun, 2014-01-05 13:01

Thanks,

Everything looks fine, although when I tested your exact code with the live feed, there was no record returned as there is no GAME-LASTMODIFIED value with todays date (2014-01-05) - the most recent record referred to yesterday (2014-01-04). When I inserted this value manually the code worked as expected.

If it is the case that you need to check for yesterday's date instead of today, then you can specify an actual time value to be used by the date() function, and the English language parsing strtodate() function to get yesterday's date, so if that's the case have a go with;

 <?php if (strpos($record["GAME-LASTMODIFIED"],(date("Y-m-d",strtotime("-1 day")))) !==false) { ?>

Hope this helps!

Cheers,
David
--
MagicParser.com

Submitted by rcasey on Mon, 2014-01-06 19:45

Thank you David. It's funny you mention strtotime, as I have played around with that and the output always works with that. However, whenever there is day-of data, the output doesn't work. My hope, with this feed, is to have it always be day-of.

Submitted by support on Tue, 2014-01-07 10:29

Hi!

I wonder if they're using a slightly different format for "today"...

If you could perhaps capture an instance of the source the next time you spot it containing a "today" record (if you view in your web browser you can use File > Save As... to save the XML) and email me a copy I'll check it out for you...

Cheers,
David
--
MagicParser.com

Submitted by rcasey on Thu, 2014-01-23 05:35

A-ha! Turns out it was a timezone issue, which I only stumbled upon by accident. Everything is working as I had hoped now. Thank you very much!