IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[DOM] Lire un flux RSS/XML


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre du Club
    Inscrit en
    Juin 2003
    Messages
    71
    Détails du profil
    Informations forums :
    Inscription : Juin 2003
    Messages : 71
    Points : 54
    Points
    54
    Par défaut [DOM] Lire un flux RSS/XML
    Bonjour,

    Je veux lire la météo de ma ville http://xml.weather.yahoo.com/forecastrss?p=SZXX0035&u=c. Voici le fichier xml que je reçois :
    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
     <channel>
     <title>Yahoo! Weather - Sion, SZ</title>
     <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sion__SZ/*http://weather.yahoo.com/forecast/SZXX0035_c.html</link>
     <description>Yahoo! Weather for Sion, SZ</description>
     <language>en-us</language>
     <lastBuildDate>Sun, 14 Oct 2007 5:00 am CEST</lastBuildDate>
     
     <ttl>60</ttl>
     <yweather:location city="Sion" region="" country="SZ" />
     <yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />
     <yweather:wind chill="6" direction="240" speed="2" />
     <yweather:atmosphere humidity="97" visibility="10" pressure="1024" rising="0" />
      <yweather:astronomy sunrise="7:47 am" sunset="6:45 pm" />
      <image>
     <title>Yahoo! Weather</title>
     
     <width>142</width>
     <height>18</height>
     <link>http://weather.yahoo.com/</link>
     <url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
     </image>
     <item>
     <title>Conditions for Sion, SZ at 5:00 am CEST</title>
     
      <geo:lat>46.22</geo:lat>
     <geo:long>7.33</geo:long>
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sion__SZ/*http://weather.yahoo.com/forecast/SZXX0035_c.html</link>
     <pubDate>Sun, 14 Oct 2007 5:00 am CEST</pubDate>
     <yweather:condition text="Fog" code="20" temp="6" date="Sun, 14 Oct 2007 5:00 am CEST" />
     <description><![CDATA[
    <img src="http://l.yimg.com/us.yimg.com/i/us/we/52/20.gif" /><br />
     <b>Current Conditions:</b><br />
     Fog, 6 C<BR /><BR />
     <b>Forecast:</b><BR />
      Sun - Sunny. High: 20 Low: 8<br />
      Mon - Sunny. High: 18 Low: 7<br />
     <br />
    <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sion__SZ/*http://weather.yahoo.com/forecast/SZXX0035_c.html">Full Forecast at Yahoo! Weather</a><BR/>
     (provided by The Weather Channel)<br/>
     ]]></description>
    	<yweather:forecast day="Sun" date="14 Oct 2007" low="8" high="20" text="Sunny" code="32">a</yweather:forecast>
    	<yweather:forecast day="Mon" date="15 Oct 2007" low="7" high="18" text="Sunny" code="32">b</yweather:forecast>
      <guid isPermaLink="false">SZXX0035_2007_10_14_5_0_CEST</guid>
     </item>
    </channel>
    </rss><!-- p2.weather.re3.yahoo.com compressed/chunked Sun Oct 14 22:54:56 PDT 2007 -->

    Je veux récupérer les 2 balise <yweather:forecast> ET LIRE leurs attributs. J'ai essayé via DOM ou parse_xml. J'accède facilement aux attributs des balises <img> ou <a> du document mais pas moyen d'accèder aux attributs des balises contenant des ":" comme <yweather:location> ou <yweather:forecast>.

    Je fais mes test sur un serveur apache 2.* avec php5.

    D'avance merci pour vos réponses et si possible exemple concret :-)

    Cordialement,
    Greg

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 777
    Points
    17 777
    Par défaut
    Le tout est de prendre en compte l'espace de nom, c'est d'autant plus facile avec une expression XPath :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <?php
    $dom = DomDocument::load('http://xml.weather.yahoo.com/forecastrss?p=SZXX0035&u=c');
    $xpath = new DomXPath($dom);
    $forecasts = $xpath->query('//yweather:forecast');
    foreach ($forecasts as $f) {
        printf('Date : %s, temps : %s<br />', $f->getAttribute('date'), $f->getAttribute('text'));
    }
    Non ?

Discussions similaires

  1. Lire un flux RSS en parsant le fichier XML
    Par michelp3 dans le forum Format d'échange (XML, JSON...)
    Réponses: 10
    Dernier message: 25/09/2014, 16h53
  2. Lire un flux RSS
    Par Houbaa dans le forum C++Builder
    Réponses: 1
    Dernier message: 25/09/2006, 15h05
  3. [ThunderBird] Refuse de lire les flux RSS depuis 3jours
    Par Swoög dans le forum Thunderbird
    Réponses: 4
    Dernier message: 27/06/2006, 21h33
  4. Comment lire un flux rss en asp
    Par ch_lensois dans le forum ASP
    Réponses: 2
    Dernier message: 20/06/2006, 11h32
  5. PHP probleme avec flux RSS, XML
    Par sirbaldur dans le forum Langage
    Réponses: 4
    Dernier message: 06/06/2006, 15h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo