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");
}
} |
Partager