1 4 import java.io.*; 5 import java.net.*; 6 import java.util.*; 7 8 import org.xmlpull.v1.*; 9 10 public class RSSReader { 11 12 public static void main(String [] args) 13 { 14 15 RSSReader rssreader = new RSSReader(); 17 18 try { 19 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 20 XmlPullParser parser = factory.newPullParser(); 21 String url = args[0]; 22 InputStreamReader stream = new InputStreamReader( 23 new URL(url).openStream()); 24 parser.setInput(stream); 25 XmlSerializer writer = factory.newSerializer(); 26 writer.setOutput(new OutputStreamWriter(System.out)); 27 rssreader.convertRSSToHtml(parser, writer); 28 } catch (Exception e) { 29 e.printStackTrace(System.err); 30 } 31 } 32 33 public void convertRSSToHtml(XmlPullParser parser, XmlSerializer writer) 34 throws IOException, XmlPullParserException 35 { 36 if (parser.nextTag() == XmlPullParser.START_TAG 38 && parser.getName().equals("rss")) 39 { 40 writer.startTag(null,"html"); 41 if (parser.nextTag() == XmlPullParser.START_TAG 42 && parser.getName().equals("channel")) 43 { 44 convertChannelToHtml(parser, writer); 45 parser.require(XmlPullParser.END_TAG, null, "channel"); 46 } else { 47 new RuntimeException ("expectd channel start tag not "+parser.getPositionDescription()); 48 } 49 parser.nextTag(); 50 parser.require(XmlPullParser.END_TAG, null, "rss"); 51 writer.endTag(null, "html"); 52 writer.flush(); 53 } else { 54 throw new RuntimeException ("expectd an RSS document at" + parser.getPositionDescription()); 55 } 56 } 57 58 public void convertChannelToHtml(XmlPullParser parser, XmlSerializer writer) 59 throws IOException, XmlPullParserException 60 { 61 boolean seenBody = false; while (parser.nextTag() != XmlPullParser.END_TAG) { if(parser.getName().equals("title") && !seenBody) { 67 writer.startTag(null,"head"); 68 writer.startTag(null,"title").text(parser.nextText()).endTag(null,"title"); 69 writer.endTag(null,"head"); 70 } else if(parser.getName().equals("item")) { 71 if(!seenBody) { 72 writer.startTag(null, "body"); 73 seenBody = true; 74 } 75 convertItemToHtml(parser, writer); 76 } else { 77 int level = 1; 79 while (level > 0) { 80 switch(parser.next()) { 81 case XmlPullParser.START_TAG: ++level; break; 82 case XmlPullParser.END_TAG: --level; break; 83 } 84 } 85 } 86 } 87 if(seenBody) writer.endTag(null, "body"); 88 } 89 90 public void convertItemToHtml(XmlPullParser parser, XmlSerializer writer) 91 throws IOException, XmlPullParserException 92 { 93 writer.startTag(null, "p"); 94 String title = null, link = null, description = null; 96 while (parser.nextTag() != XmlPullParser.END_TAG) { 97 if (parser.getName().equals("title")) { 98 title = parser.nextText(); 99 } else if (parser.getName().equals("link")) { 100 link = parser.nextText(); 101 } else if (parser.getName().equals("description")) { 102 description = parser.nextText(); 103 } 104 } 105 106 writer.startTag(null, "a"); 110 writer.attribute(null, "href", link); 111 if(title != null) { 112 writer.text(title); 113 } else { 114 writer.text(link); 115 } 116 writer.endTag(null,"a"); 117 118 writer.startTag(null, "br").endTag(null, "br"); 120 121 if(description != null) writer.text(description); 122 123 writer.endTag(null, "p"); } 125 } 126 127 | Popular Tags |