2010-04-21 2 views
0

Avec un flux XML comme ceci:XML RSS Feed PHP Parse

<w:current temperature="22.2" dewPoint="12.9" humidity="56" windSpeed="5.6" windGusts="9.3" windDirection="ESE" pressure="1017.8" rain="0.0" />

et

<w:forecast day="Thursday" description="Mostly Sunny. Warm." min="17" max="29" icon="2" iconUri="http://www.weather.com.au/images/icons/2.gif" iconAlt="Mostly Sunny" />

Comment puis-je analyser en PHP avec le Royaume?

$doc = new DOMDocument(); 
$doc->load('http://rss.weather.com.au/sa/adelaide'); 
$arrFeeds = array(); 
foreach ($doc->getElementsByTagName('item') as $node) { 
    $itemRSS = array ( 
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
     'description' => $node->getElementsByTagName('w')->item(0)->nodeValue, 
     ); 
    array_push($arrFeeds, $itemRSS); 
} 

erreur de retour: Notice: Trying to get property of non-object in /var/www/index.php on line 123

Répondre

3

Je personnellement utilise SimpleXML pour cela, il vous suffit de garder à l'esprit l'espace de noms personnalisé qui est utilisé pour transporter les données météorologiques, voici un petit exemple (avec pas de gestion des erreurs etc) que je viens de tester et récupère le temps actuel.

//Fetch the feed 
$feed = file_get_contents("http://rss.weather.com.au/sa/adelaide"); 
//Load it into simplexml 
$weather = simplexml_load_string($feed); 
//Get namespace descendants using the w namespace defined in the feed 
$channelelements = $weather->channel->item->children("http://rss.weather.com.au/w.dtd"); 
//Looping through each of the attributes and echoing them, you can do what you want with them at this point 
foreach($channelelements->attributes() as $k => $attr) { 
    echo $k.' = '.$attr.'<br />'; 
}