package rsstray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Statement;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class RssReader {
/**
* @param args
*/
public static void main(String[] args) {
StringBuffer strbuf = new StringBuffer();
try {
// Create a URL for the desired page
URL url = new URL("http://9mmo.com/games/rss.php");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
String str;
while ((str = in.readLine()) != null) {
strbuf.append(str+"\n");
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
String xmlstr = strbuf.toString();
System.err.println(xmlstr);
Statement stm=null;
Document document=null;
try {
document = DocumentHelper.parseText(xmlstr);
} catch (DocumentException e) {
System.err.print("XML:"+xmlstr);
e.printStackTrace();
}
document.setXMLEncoding("GBK");
Element root = document.getRootElement();
root = root.element("channel");
for ( Iterator i = root.elementIterator("item"); i.hasNext(); ) {
Element foo = (Element) i.next();
String title = foo.element("title").getText();
String link = foo.element("link").getText();
String description = foo.element("description").getText();
System.out.println(title + "->" +link + ":::" +description);
// do something
}
}
}



Leave a reply