Java et rss

Fermé
Dr_Mckay Messages postés 47 Date d'inscription dimanche 11 septembre 2011 Statut Membre Dernière intervention 23 avril 2012 - 5 avril 2012 à 12:13
Bonjour,

J'ai un code qui permet de lire le rss il fonctionne mais je n'arrive pas à l'intégrer dans une fenêtre et pas pour une app console !

Voila le code de la classe RSSReader :

package net.minecraft;

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 RSSReader {
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;
nodes = doc.getElementsByTagName("title");
Node node = doc.getDocumentElement();
System.out.println("Flux RSS: " + this.readNode(node, "channel|title"));
System.out.println("Date de publication: " + GMTDateToFrench(this.readNode(node, "channel|lastBuildDate")));
System.out.println();
nodes = doc.getElementsByTagName("item");
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("Date: " + GMTDateToFrench(readNode(element, "pubDate")));
System.out.println("Description: " + readNode(element, "description"));
System.out.println();
} //for
//for
} catch (SAXException ex) {
Logger.getLogger(RSSReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(RSSReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(RSSReader.class.getName()).log(Level.SEVERE, null, ex);
}
}

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

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;
}

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(RSSReader.class.getName()).log(Level.SEVERE, null, ex);
}
return "";
}
}
Et voilà le code qui ne fonctionne pas (sencer l'afficher dans ma fenêtre et plus précisément dans un Jtextpane) :
try
{
RSSReader reader = new RSSReader();
editorPane = reader.parse("https://www.000webhost.com/migrate?static=true");
}
catch (Exception e)
{
e.printStackTrace();
editorPane.setText("Impossible de charger les news du serveur..." + e.toString() + "</center></font></body></html>");
}


A voir également: