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

Format d'échange (XML, JSON...) Java Discussion :

Lire un flux RSS en parsant le fichier XML


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    315
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2009
    Messages : 315
    Points : 114
    Points
    114
    Par défaut Lire un flux RSS en parsant le fichier XML
    Bonjour,

    comme le titre l'indique, j'essaie de lire un flux RSS avec XML

    le Programme fonctionne bien avec l'adresse suivante :
    reader.parse("http://fobec.com/CMS/fobec.xml");

    MAIS pas avec
    reader.parse("http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml");

    Message d'erreur :
    Flux RSS:
    23-sept.-2014 16:18:54 lire_RSS.CopyOfRSSReader GMTDateToFrench
    GRAVE: null
    java.text.ParseException: Unparseable date: ""
    at java.text.DateFormat.parse(DateFormat.java:337)
    at lire_RSS.CopyOfRSSReader.GMTDateToFrench(CopyOfRSSReader.java:127)
    at lire_RSS.CopyOfRSSReader.parse(CopyOfRSSReader.java:43)
    at lire_RSS.CopyOfRSSReader.main(CopyOfRSSReader.java:143)
    Date de publication:
    Flux RSS:

    Voici le programme en question
    Merci pour vos suggestions
    Code : 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
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
     
    package lire_RSS;
     
    import java.io.IOException;
    import java.net.URL;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
     
    public class CopyOfRSSReader {
     
        /**
         * Parser le fichier XML
         * @param feedurl URL du flux RSS
         */
        public void parse(String feedurl) {
            try {
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                URL url = new URL(feedurl);
                Document doc = builder.parse(url.openStream());
                NodeList nodes = null;
                Element element = null;
                /**
                 * Titre et date du flux
                 */
                nodes = doc.getElementsByTagName("title");
                Node node = doc.getDocumentElement();
                System.out.println("Flux RSS: " + this.readNode(node, "channel|link"));
                System.out.println("Date de publication: " + GMTDateToFrench(this.readNode(node, "channel|lastBuildDate")));
                System.out.println("Flux RSS: " + this.readNode(node, "channel|title"));
     
                System.out.println();
                /**
                 * Elements du flux RSS
                 **/
                nodes = doc.getElementsByTagName("item");
                for (int i = 0; i < nodes.getLength(); i++) {
                    element = (Element) nodes.item(i);
                    System.out.println("Lien: " + readNode(element, "link"));
                    System.out.println("Titre: " + readNode(element, "title"));
                    System.out.println("Description: " + readNode(element, "description"));
                    System.out.println();
                } //for
                //for
            } catch (SAXException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
        /**
         * Méthode permettant de retourner ce que contient d'un noeud
         * @param _node le noeud principal
         * @param _path suite des noms des noeud sans espace séparer par des "|"
         * @return un string contenant le valeur du noeud voulu
         */
        public String readNode(Node _node, String _path) {
     
            String[] paths = _path.split("\\|");
            Node node = null;
     
            if (paths != null && paths.length > 0) {
                node = _node;
     
                for (int i = 0; i < paths.length; i++) {
                    node = getChildByName(node, paths[i].trim());
                }
            }
     
            if (node != null) {
                return node.getTextContent();
            } else {
                return "";
            }
        }
     
        /**
         * renvoye le nom d'un noeud fils a partir de son nom
         * @param _node noeud pricipal
         * @param _name nom du noeud fils
         * @return le noeud fils
         */
        public Node getChildByName(Node _node, String _name) {
            if (_node == null) {
                return null;
            }
            NodeList listChild = _node.getChildNodes();
     
            if (listChild != null) {
                for (int i = 0; i < listChild.getLength(); i++) {
                    Node child = listChild.item(i);
                    if (child != null) {
                        if ((child.getNodeName() != null && (_name.equals(child.getNodeName()))) || (child.getLocalName() != null && (_name.equals(child.getLocalName())))) {
                            return child;
                        }
                    }
                }
            }
            return null;
        }
     
        /**
         * Afficher une Date GML au format francais
         * @param gmtDate
         * @return
         */
        public String GMTDateToFrench(String gmtDate) {
            try {
                SimpleDateFormat dfGMT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
                dfGMT.parse(gmtDate);
                SimpleDateFormat dfFrench = new SimpleDateFormat("EEEE, d MMMM yyyy HH:mm:ss", Locale.FRANCE);
                return dfFrench.format(dfGMT.getCalendar().getTime());
            } catch (ParseException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
     
        /**
         * Exemple
         * @param args
         */
        public static void main(String[] args) {
            CopyOfRSSReader reader = new CopyOfRSSReader();
            //reader.parse("http://fobec.com/CMS/fobec.xml");
            reader.parse("http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml");
     
        }
    }

  2. #2
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 710
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 710
    Points : 4 794
    Points
    4 794
    Par défaut
    Comme indiqué dans le stack d'erreur, il s'agit d'un problème de format date

    date dans le 1er site :
    Thu, 18 Sep 2014 09:19:19 GMT
    pour cela tu utilises : new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH) et ça marche ...

    date dans le 2ème site :
    2014-09-22T17:57:02Z
    pour cela tu utilises le même format : new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH) et ça marche pas ! ...

    donc tu ne peux pas utiliser le même format de date
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    315
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2009
    Messages : 315
    Points : 114
    Points
    114
    Par défaut
    merci de ta réponse

    OK , il y a un problème de date mais passons ( je la mets en commentaire)

    ce qui me pose vraiment un problème , c'est que je n 'obtiens pas de résultat pour

    System.out.println("Flux RSS: " + this.readNode(node, "channel|link"));
    et
    System.out.println("Flux RSS: " + this.readNode(node, "channel|title"));

    rien non plus pour les items

  4. #4
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 710
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 710
    Points : 4 794
    Points
    4 794
    Par défaut
    Dans le 1er site, la balise XML chanel existe
    mais pas dans le second site. Donc ça ne peut pas fonctionner !

    Il faut que tu regardes le code source de la page de tes sites pour voir les balises utilisées (ainsi que le format des données d'ailleurs)

    A l'armée, on t'aurait dit : "même motif, même punition ..."
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    315
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2009
    Messages : 315
    Points : 114
    Points
    114
    Par défaut
    la balise <channel est là

    voici le source

    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
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
     
    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005">
    <channel xmlns:cfi="http://www.microsoft.com/schemas/rss/core/2005/internal" cfi:lastdownloaderror="None">
    <link>http://earthquake.usgs.gov/earthquakes/</link>
    <atom:updated>2014-09-22T14:28:53Z</atom:updated>
    <lastBuildDate>Mon, 22 Sep 2014 14:28:53 GMT</lastBuildDate>
    <title cf:type="text">USGS M 2.5+ Earthquakes</title>
    <description cf:type="text">Real-time, worldwide earthquake list for the past day</description>
    <atom:link href="http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml" rel="self"/>
    <atom:link href="http://earthquake.usgs.gov/earthquakes/"/>
    <managingEditor>U.S. Geological Survey</managingEditor>
    <atom:author><atom:name>U.S. Geological Survey</atom:name>
    </atom:author><cf:guid isPermaLink="false">http://earthquake.usgs.gov/</cf:guid>
    <item>
    	<link>http://earthquake.usgs.gov/earthquakes/catalogs/</link>
    	<guid isPermaLink="false">urn:earthquake-usgs-gov:announcement-deprecated</guid>
    	<title xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="text">Data Feed Deprecated</title>
    	<atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2014-09-22T14:28:53Z</atom:updated>
    	<atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="http://earthquake.usgs.gov/earthquakes/catalogs/" rel="alternate" type="text/html"/>
    	<description xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="html">&amp;lt;p style=&amp;quot;font-weight:bold;color:#900;&amp;quot;&amp;gt;This USGS data file has been deprecated.&amp;lt;/p&amp;gt;&amp;lt;p&amp;gt;To continue receiving updates for earthquake information you must switch to the &amp;lt;a href=&amp;quot;<a href="http://earthquake.usgs.gov/earthquakes/feed/&amp;quot;&amp;gt;new" target="_blank">http://earthquake.usgs.gov/earthquak...ot;&amp;gt;new</a> data
    		format&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;&amp;lt;p&amp;gt;In the future, data feeds will be updated and deprecated following our &amp;lt;a href=&amp;quot;<a href="http://earthquake.usgs.gov/earthquakes/feed/policy.php&amp;quot;&amp;gt;official" target="_blank">http://earthquake.usgs.gov/earthquak...mp;gt;official</a> deprecation policy&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;</description>
    	<cfi:id>4</cfi:id>
    	<cfi:effectiveId>3101266789</cfi:effectiveId>
    	<cfi:read>true</cfi:read>
    	<cfi:downloadurl>http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml</cfi:downloadurl>
    	<cfi:lastdownloadtime>2014-09-22T14:32:58.978Z</cfi:lastdownloadtime>
    </item>
    <item>
    	<link>http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/uw60884306.php</link>
    	<guid isPermaLink="false">urn:earthquake-usgs-gov:uw:60884306</guid>
    	<title xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="text">M 2.5, Nevada</title>
    	<atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2014-09-22T02:28:54Z</atom:updated>
    	<atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/uw60884306.php" rel="alternate" type="text/html"/>
    	<description xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="html">&lt;img src="http://earthquake.usgs.gov/images/globes/40_-120.jpg" alt="41.843°N 119.706°W" align=left hspace=20&gt;&lt;p&gt;Monday, September 22, 2014 02:28:54 UTC&lt;br&gt;Sunday, September 21, 2014 07:28:54 PM at epicenter&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Depth&lt;/strong&gt;: 0.00 km (0.00 mi)&lt;/p&gt;</description>
    	<georss:point xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">41.8433 -119.7057</georss:point>
    	<georss:elev xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">-0</georss:elev>
    	<category>Past day</category>
    	<cfi:id>3</cfi:id>
    	<cfi:effectiveId>2022970456</cfi:effectiveId>
    	<cfi:read>true</cfi:read>
    	<cfi:downloadurl>http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml</cfi:downloadurl>
    	<cfi:lastdownloadtime>2014-09-22T14:32:58.978Z</cfi:lastdownloadtime>
    </item>
    <item>
    	<link>http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265001.php</link>
    	<guid isPermaLink="false">urn:earthquake-usgs-gov:pr:14265001</guid>
    	<title xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="text">M 3.4, Puerto Rico region</title>
    	<atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2014-09-22T02:16:10Z</atom:updated>
    	<atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265001.php" rel="alternate" type="text/html"/>
    	<description xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="html">&lt;img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.659°N 65.590°W" align=left hspace=20&gt;&lt;p&gt;Monday, September 22, 2014 02:16:10 UTC&lt;br&gt;Sunday, September 21, 2014 10:16:10 PM at epicenter&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Depth&lt;/strong&gt;: 43.00 km (26.72 mi)&lt;/p&gt;</description>
    	<georss:point xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">19.6588 -65.5897</georss:point>
    	<georss:elev xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">-43000</georss:elev>
    	<category>Past day</category>
    	<cfi:id>2</cfi:id>
    	<cfi:effectiveId>1550548798</cfi:effectiveId>
    	<cfi:read>true</cfi:read>
    	<cfi:downloadurl>http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml</cfi:downloadurl>
    	<cfi:lastdownloadtime>2014-09-22T14:32:58.978Z</cfi:lastdownloadtime>
    </item>
    <item>
    	<link>http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14264013.php</link>
    	<guid isPermaLink="false">urn:earthquake-usgs-gov:pr:14264013</guid>
    	<title xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="text">M 2.8, Virgin Islands region</title>
    	<atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2014-09-21T21:24:20Z</atom:updated>
    	<atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14264013.php" rel="alternate" type="text/html"/>
    	<description xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="html">&lt;img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="18.922°N 64.960°W" align=left hspace=20&gt;&lt;p&gt;Sunday, September 21, 2014 21:24:20 UTC&lt;br&gt;Sunday, September 21, 2014 05:24:20 PM at epicenter&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Depth&lt;/strong&gt;: 45.00 km (27.96 mi)&lt;/p&gt;</description>
    	<georss:point xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">18.9220 -64.9600</georss:point>
    	<georss:elev xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">-45000</georss:elev>
    	<category>Past day</category>
    	<cfi:id>1</cfi:id>
    	<cfi:effectiveId>126265246</cfi:effectiveId>
    	<cfi:read>true</cfi:read>
    	<cfi:downloadurl>http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml</cfi:downloadurl>
    	<cfi:lastdownloadtime>2014-09-22T14:32:58.978Z</cfi:lastdownloadtime>
    </item>
    <item>
    	<link>http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14264008.php</link>
    	<guid isPermaLink="false">urn:earthquake-usgs-gov:pr:14264008</guid>
    	<title xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="text">M 2.9, Puerto Rico region</title>
    	<atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2014-09-21T17:59:15Z</atom:updated>
    	<atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14264008.php" rel="alternate" type="text/html"/>
    	<description xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" cf:type="html">&lt;img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.386°N 65.387°W" align=left hspace=20&gt;&lt;p&gt;Sunday, September 21, 2014 17:59:15 UTC&lt;br&gt;Sunday, September 21, 2014 01:59:15 PM at epicenter&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Depth&lt;/strong&gt;: 110.00 km (68.35 mi)&lt;/p&gt;</description>
    	<georss:point xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">19.3857 -65.3874</georss:point>
    	<georss:elev xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">-110000</georss:elev>
    	<category>Past day</category>
    	<cfi:id>0</cfi:id>
    	<cfi:effectiveId>3828836918</cfi:effectiveId>
    	<cfi:read>true</cfi:read>
    	<cfi:downloadurl>http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml</cfi:downloadurl>
    	<cfi:lastdownloadtime>2014-09-22T14:32:58.978Z</cfi:lastdownloadtime>
    	</item>
    	</channel>
    	</rss>

  6. #6
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    En allant sur l'URL http://earthquake.usgs.gov/eqcenter/.../1day-M2.5.xml que tu nous as montrée, j'obtiens ce code-ci :

    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
    <?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">
      <updated>2014-09-23T15:43:16Z</updated>
      <title>USGS M 2.5+ Earthquakes</title>
      <subtitle>Real-time, worldwide earthquake list for the past day</subtitle>
      <link rel="self" href="http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml"/>
      <link href="http://earthquake.usgs.gov/earthquakes/"/>
      <author><name>U.S. Geological Survey</name></author>
      <id>http://earthquake.usgs.gov/</id>
      <icon>/favicon.ico</icon>
    <entry><id>urn:earthquake-usgs-gov:announcement-deprecated</id><title>Data Feed Deprecated</title><updated>2014-09-23T15:43:16Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/catalogs/"/><summary><![CDATA[<p style="font-weight:bold;color:#900;">This USGS data file has been deprecated.</p><p>To continue receiving updates for earthquake information you must switch to the <a href="http://earthquake.usgs.gov/earthquakes/feed/">new data
    	format</a>.</p><p>In the future, data feeds will be updated and deprecated following our <a href="http://earthquake.usgs.gov/earthquakes/feed/policy.php">official deprecation policy</a>.</p>]]></summary></entry>  <entry><id>urn:earthquake-usgs-gov:pr:14266001</id><title>M 2.7, Puerto Rico region</title><updated>2014-09-23T07:13:13Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14266001.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="18.584°N 65.521°W" align="left" hspace="20" /><p>Tuesday, September 23, 2014 07:13:13 UTC<br>Tuesday, September 23, 2014 03:13:13 AM at epicenter</p><p><strong>Depth</strong>: 23.00 km (14.29 mi)</p>]]></summary><georss:point>18.5839 -65.5205</georss:point><georss:elev>-23000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14266000</id><title>M 3.2, north of the Virgin Islands</title><updated>2014-09-23T01:30:26Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14266000.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.947°N 64.257°W" align="left" hspace="20" /><p>Tuesday, September 23, 2014 01:30:26 UTC<br>Monday, September 22, 2014 09:30:26 PM at epicenter</p><p><strong>Depth</strong>: 71.00 km (44.12 mi)</p>]]></summary><georss:point>19.9465 -64.2565</georss:point><georss:elev>-71000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265011</id><title>M 2.7, Virgin Islands region</title><updated>2014-09-22T17:57:02Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265011.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.003°N 64.919°W" align="left" hspace="20" /><p>Monday, September 22, 2014 17:57:02 UTC<br>Monday, September 22, 2014 01:57:02 PM at epicenter</p><p><strong>Depth</strong>: 28.00 km (17.40 mi)</p>]]></summary><georss:point>19.0030 -64.9188</georss:point><georss:elev>-28000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265009</id><title>M 2.7, Virgin Islands region</title><updated>2014-09-22T17:54:24Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265009.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.019°N 64.914°W" align="left" hspace="20" /><p>Monday, September 22, 2014 17:54:24 UTC<br>Monday, September 22, 2014 01:54:24 PM at epicenter</p><p><strong>Depth</strong>: 30.00 km (18.64 mi)</p>]]></summary><georss:point>19.0188 -64.9140</georss:point><georss:elev>-30000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265012</id><title>M 2.7, Virgin Islands region</title><updated>2014-09-22T17:50:12Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265012.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="18.997°N 64.937°W" align="left" hspace="20" /><p>Monday, September 22, 2014 17:50:12 UTC<br>Monday, September 22, 2014 01:50:12 PM at epicenter</p><p><strong>Depth</strong>: 30.00 km (18.64 mi)</p>]]></summary><georss:point>18.9972 -64.9371</georss:point><georss:elev>-30000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265005</id><title>M 2.5, Virgin Islands region</title><updated>2014-09-22T17:39:37Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265005.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="18.973°N 64.952°W" align="left" hspace="20" /><p>Monday, September 22, 2014 17:39:37 UTC<br>Monday, September 22, 2014 01:39:37 PM at epicenter</p><p><strong>Depth</strong>: 19.00 km (11.81 mi)</p>]]></summary><georss:point>18.9727 -64.9523</georss:point><georss:elev>-19000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265004</id><title>M 3.0, Virgin Islands region</title><updated>2014-09-22T17:36:28Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265004.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="18.973°N 64.919°W" align="left" hspace="20" /><p>Monday, September 22, 2014 17:36:28 UTC<br>Monday, September 22, 2014 01:36:28 PM at epicenter</p><p><strong>Depth</strong>: 29.00 km (18.02 mi)</p>]]></summary><georss:point>18.9726 -64.9190</georss:point><georss:elev>-29000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265007</id><title>M 2.8, Virgin Islands region</title><updated>2014-09-22T17:26:12Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265007.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.037°N 64.619°W" align="left" hspace="20" /><p>Monday, September 22, 2014 17:26:12 UTC<br>Monday, September 22, 2014 01:26:12 PM at epicenter</p><p><strong>Depth</strong>: 52.00 km (32.31 mi)</p>]]></summary><georss:point>19.0370 -64.6190</georss:point><georss:elev>-52000</georss:elev><category label="Age" term="Past day"/></entry>
      <entry><id>urn:earthquake-usgs-gov:pr:14265006</id><title>M 2.6, Puerto Rico region</title><updated>2014-09-22T16:56:35Z</updated><link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr14265006.php"/><summary type="html"><![CDATA[<img src="http://earthquake.usgs.gov/images/globes/20_-65.jpg" alt="19.646°N 65.534°W" align="left" hspace="20" /><p>Monday, September 22, 2014 16:56:35 UTC<br>Monday, September 22, 2014 12:56:35 PM at epicenter</p><p><strong>Depth</strong>: 50.00 km (31.07 mi)</p>]]></summary><georss:point>19.6455 -65.5339</georss:point><georss:elev>-50000</georss:elev><category label="Age" term="Past day"/></entry>
    </feed>

    Il n'y a pas de <channel>.

    Ce n'est pas cette URL que tu utilises, finalement ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    315
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2009
    Messages : 315
    Points : 114
    Points
    114
    Par défaut se tromper çà peut auusi aider
    comme quoi çà sert parfois de se tromper ....

    j'ai pris le source qui se trouve ds flux de mon navigateur

    sans vous je ne m'en serais pas aperçu ...Merci bcp

    Mais grâce à cela je vais pouvoir rebondir ... sur un exercice android qui m'a transporté sur les parseurs XML

    je vous tiens au courant

    a demain

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    315
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2009
    Messages : 315
    Points : 114
    Points
    114
    Par défaut
    Bonjour,

    j'ai revu la question et j'ai modifié mon programme qui tourne

    Mais je n'arrive pas à afficher le contenu de la balise link

    Je pense que c'est parce que elle est conçue comme ceci :<link ...../>

    et pas <link> .... </link>

    QUID pour que çà marche , Merci pour vos réponses

    voici le code
    Code : 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
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
     
    package lire_RSS;
     
    // copy de RSSReader pour montrer sur Developpez
     
    import java.io.IOException;
    import java.net.URL;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
     
    public class CopyOfRSSReader {
     
        /**
         * Parser le fichier XML
         * @param feedurl URL du flux RSS
         */
        public void parse(String feedurl) {
            try {
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                URL url = new URL(feedurl);
                Document doc = builder.parse(url.openStream());
                NodeList nodes = null;
                Element element = null;
                /**
                 * Titre et date du flux
                 */
                Node node = doc.getDocumentElement();
                System.out.println("Flux RSS: " + this.readNode(node, "title"));
                System.out.println("Flux RSS: " + this.readNode(node, "link"));
                //System.out.println("Date de publication: " + GMTDateToFrench(this.readNode(node, "channel|lastBuildDate")));
     
     
                System.out.println();
                /**
                 * Elements du flux RSS
                 **/
                nodes = doc.getElementsByTagName("entry");
                for (int i = 0; i < nodes.getLength(); i++) {
                    element = (Element) nodes.item(i);
                    System.out.println("Titre: " + readNode(element, "title"));
                    System.out.println("Lien: " + readNode(element, "link"));
                    System.out.println("Description: " + readNode(element, "summary"));
                    System.out.println();
                } //for
                //for
            } catch (SAXException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
        /**
         * Méthode permettant de retourner ce que contient d'un noeud
         * @param _node le noeud principal
         * @param _path suite des noms des noeud sans espace séparer par des "|"
         * @return un string contenant le valeur du noeud voulu
         */
        public String readNode(Node _node, String _path) {
     
            String[] paths = _path.split("\\|");
            Node node = null;
     
            if (paths != null && paths.length > 0) {
                node = _node;
     
                for (int i = 0; i < paths.length; i++) {
                    node = getChildByName(node, paths[i].trim());
                }
            }
     
            if (node != null) {
                return node.getTextContent();
            } else {
                return "";
            }
        }
     
        /**
         * renvoye le nom d'un noeud fils a partir de son nom
         * @param _node noeud pricipal
         * @param _name nom du noeud fils
         * @return le noeud fils
         */
        public Node getChildByName(Node _node, String _name) {
            if (_node == null) {
                return null;
            }
            NodeList listChild = _node.getChildNodes();
     
            if (listChild != null) {
                for (int i = 0; i < listChild.getLength(); i++) {
                    Node child = listChild.item(i);
                    if (child != null) {
                        if ((child.getNodeName() != null && (_name.equals(child.getNodeName()))) || (child.getLocalName() != null && (_name.equals(child.getLocalName())))) {
                            return child;
                        }
                    }
                }
            }
            return null;
        }
     
        /**
         * Afficher une Date GML au format francais
         * @param gmtDate
         * @return
         */
        public String GMTDateToFrench(String gmtDate) {
            try {
                SimpleDateFormat dfGMT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
                dfGMT.parse(gmtDate);
                SimpleDateFormat dfFrench = new SimpleDateFormat("EEEE, d MMMM yyyy HH:mm:ss", Locale.FRANCE);
                return dfFrench.format(dfGMT.getCalendar().getTime());
            } catch (ParseException ex) {
                Logger.getLogger(CopyOfRSSReader.class.getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
     
        /**
         * Exemple
         * @param args
         */
        public static void main(String[] args) {
            CopyOfRSSReader reader = new CopyOfRSSReader();
            reader.parse("http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml");
     
     
        }
    }

  9. #9
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    href est ce que l'on appelle un attribut. Je suggère getAttribute().

    Note : lire du XML serait infiniment plus simple avec la bibliothèque tierce JDOM.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Novembre 2009
    Messages
    315
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2009
    Messages : 315
    Points : 114
    Points
    114
    Par défaut
    bonjour,

    la piste NamedNodeMap attrs = child.getAttributes();

    s'avère être la bonne

    Merci

  11. #11
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 551
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 551
    Points : 21 607
    Points
    21 607
    Par défaut
    Euh.... Ouais, question de goûts je suppose, en étant très ouvert.
    Il y avait un problème avec le très simple getAttribute("href") ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème pour lire les flux rss en flex
    Par gannher dans le forum Flex
    Réponses: 6
    Dernier message: 18/07/2007, 16h24
  2. [RSS] Lire un flux RSS sur son site perso
    Par nicofonk dans le forum Langage
    Réponses: 5
    Dernier message: 22/11/2006, 22h55
  3. Lire un flux RSS
    Par Houbaa dans le forum C++Builder
    Réponses: 1
    Dernier message: 25/09/2006, 15h05
  4. [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
  5. Comment lire un flux rss en asp
    Par ch_lensois dans le forum ASP
    Réponses: 2
    Dernier message: 20/06/2006, 11h32

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