You are here:  » Unable to parse


Unable to parse

Submitted by GeXus on Thu, 2007-03-29 04:24 in

I'm having trouble getting all of the fields from this feed - specifically the aws:xxx fields, any ideas? Thanks!

http://api.wxbug.net/getForecastRSS.aspx?ACode=A5473690772&zipCode=56395&unittype=0

Submitted by support on Thu, 2007-03-29 07:39

Hi,

There are 2 aspects of this feed that you need to consider. Firstly, Magic Parser by default (using autodetection) tries to parse out the most frequently repeating record; which in this feed are the main RSS items. The aws: weather details that you are trying to access are higher up in the document, so a different format string is required. The format string you need to access the top level parameters is:

xml|RSS/CHANNEL/

Secondly, having now obtained the main channel info into your record handler function as the $record array, the next issue is that you need to handle the @n postfix that Magic Parser appends to field names to resolve fields that would otherwise have the same name within the current record. This involves creating a loop through @1, @2 etc. and testing to see whether the repeating field with that postfix exists - and then using the values if so.

It's easier to explain with an example, so here is a script to read this feed running on this server:

http://www.magicparser.com/examples/weather.php

Here's the source:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$city $record["AWS:WEATHER/AWS:FORECASTS/AWS:LOCATION/AWS:CITY"];
    
$state $record["AWS:WEATHER/AWS:FORECASTS/AWS:LOCATION/AWS:STATE"];
    print 
"<h3>Weather Forecast for ".$city.",".$state."</h3>";
    
$i 0;
    while(
1) {
      if (
$i$postfix "@".$i;
      if (!isset(
$record["AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST".$postfix])) break;
      
$day $record["AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST/AWS:TITLE".$postfix];
      
$pre $record["AWS:WEATHER/AWS:FORECASTS/AWS:FORECAST/AWS:SHORT-PREDICTION".$postfix];
      print 
"<p>".$day." - ".$pre."</p>";
      
$i++;
    }
  }
  
$url "http://api.wxbug.net/getForecastRSS.aspx?ACode=A5473690772&zipCode=56395&unittype=0";
  
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/");
?>

Within the loop i've just extracted out the AWS:TITLE and AWS:SHORT-PREDICTION fields but you can of course access all the other weather fields in exactly the same way. Outside the loop, I have used the AWS:CITY and AWS:STATE fields to display the location of the forecast.

Hope this helps!
Cheers,
David.

Submitted by GeXus on Thu, 2007-03-29 13:46

That's excellent, thank you!