Nice ... det var "bare" det der skulle til...
package RSSReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
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;
import java.net.*;
public class ParseW3CDOM {
//URL url = new URL("
http://www.webcafe.dk/debat/java.rss");
// private final static String XML_FILE = "C:\\seneste10.xml";
public static void main(String[] args) throws MalformedURLException {
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "ISAPROXY.hih.dk");
System.setProperty("http.proxyPort", "8080");
URL URL_FILE = new URL("
http://www.ekstrabladet.dk/seneste10.rss");
InputStream XML_FILE = null;
try {
XML_FILE = URL_FILE.openStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// læs fra fil til DOM træ
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(XML_FILE);//new File(XML_FILE));
// lav liste over alle elementer af typen 'item'
NodeList elements = doc.getElementsByTagName("item");
for (int i = 0; i < elements.getLength(); i++) {
Node element = (Element) elements.item(i);
// find attributten item
String item = ((Element) element).getAttribute("item");
String title = "";
String description = "";
String link = "";
String date = "";
String creator = "";
// lav liste over alle childs og find title, description, link, date og creator
NodeList subelements = element.getChildNodes();
for (int j = 0; j < subelements.getLength(); j++) {
String tag = subelements.item(j).getNodeName();
if (tag.equals("dc:date")) {
date = subelements.item(j).getFirstChild().getNodeValue();
}
if (tag.equals("dc:creator")) {
creator = subelements.item(j).getFirstChild().getNodeValue();
}
if (tag.equals("title")) {
title = subelements.item(j).getFirstChild().getNodeValue();
}
if (tag.equals("description")) {
description = subelements.item(j).getFirstChild().getNodeValue();
}
if (tag.equals("link")) {
link = subelements.item(j).getFirstChild().getNodeValue();
}
}
System.out.println("item=" + item);
System.out.println("title=" + title);
System.out.println("description=" + description);
System.out.println("link=" + link);
System.out.println("date=" + date);
System.out.println("creator=" + creator);
}
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}