10. สิ่งผิดปกติ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 10-13  ตัวอย่างการใช้ WebPage เพื่อแสดงสรุปข่าวการเมืองจากสำนักข่าวไทย
import java.util.Scanner;

public class SimpleNewsAggregator {
  public static void main(String[] args) {
    String page = " http://news.mcot.net/news_rss/politic.xml";  
    try {
      WebPage webpage = new WebPage(page);
      Scanner sc = webpage.getScanner();
      int start, end;
      int n = 1;
      boolean itemFound = false;
      while (sc.hasNext()) {
        String line = sc.nextLine();
        if (line.indexOf("<item>") >= 0) itemFound = true;
        if (itemFound) {
          if ((start = line.indexOf("<title>")) >= 0) {
            if ((end = line.indexOf("</title>")) >= 0) {
              String title = line.substring(start+7, end);
              System.out.println((n++) + ": \t" + title);
            }
            itemFound = false;
          }
        }
      }
    } catch (WebPageException e) {
      System.out.println("มีปัญหาเมื่ออ่านเว็บเพจ : " + e.getMessage());
    }
  }
}
©2009 S.Prasitjutrakul