用户操作
[即时聊天] [发私信] [加为好友]
夏纯中ID:danny_xcz
857077次访问,排名41好友2人,关注者84
danny_xcz的文章
原创 296 篇
翻译 3 篇
转载 25 篇
评论 638 篇
纯月的公告
最近评论
zjxzou:bucuo~
yu3350360:收藏了先 呵呵
henhaohll:有些意思啊!呵呵 ..
sap99:www.sap99.com/,SAP99资料多多

SAP免费资料下载
http://www.sap99.com

有很多的学习资料,推荐一下,
snowring:你好,能留下联系方式吗?
我也在研究MULE,QQ:7766284
谢谢了。
文章分类
收藏
    相册
    Blog用途
    我的相册
    Java Desktop
    Open Source
    友情链接
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 使用htmlparser获取sohu的天气预报收藏

    新一篇: JBPM的任务分派机制 | 旧一篇: SVN安装和使用(新一代的CVS)

    首先,我们新建一个类,存放天气信息

    /*
     * Created on 2005-3-8
     *
     * To change the template for this generated file go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    package org.exoplatform.portlets.chinaweather.component;

    /**
     * @author Administrator
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    public class Weather {
     private String city;
     private String state;
     private String temperature;
     private String time;
     private String wind;
     private String windpower;
     private long UpdateTime;

     /**
      * @return
      */
     public String getTemperature() {
      return temperature;
     }

     /**
      * @return
      */
     public String getTime() {
      return time;
     }

     /**
      * @return
      */
     public String getWind() {
      return wind;
     }

     /**
      * @return
      */
     public String getWindpower() {
      return windpower;
     }

     /**
      * @param string
      */
     public void setTemperature(String string) {
      temperature = string;
     }

     /**
      * @param string
      */
     public void setTime(String string) {
      time = string;
     }

     /**
      * @param string
      */
     public void setWind(String string) {
      wind = string;
     }

     /**
      * @param string
      */
     public void setWindpower(String string) {
      windpower = string;
     }

     /**
      * @return
      */
     public long getUpdateTime() {
      return UpdateTime;
     }

     /**
      * @param l
      */
     public void setUpdateTime(long l) {
      UpdateTime = l;
     }

     /**
      * @return
      */
     public String getState() {
      return state;
     }

     /**
      * @param string
      */
     public void setState(String string) {
      state = string;
     }

     /**
      * @return
      */
     public String getCity() {
      return city;
     }

     /**
      * @param string
      */
     public void setCity(String string) {
      city = string;
     }

    }

    具体的解析代码为:

    private Weather parserWeather() throws Exception {
      Weather w = new Weather();
      try {

       //Parser parser =
       // new Parser("file://localhost/I:/projects/query.html");
       Parser parser =
        new Parser("http://weather.news.sohu.com/query.php?city=镇江");
       
       parser.setEncoding("GBK");
       Node nodes[] = parser.extractAllNodesThatAre(TableTag.class);

       TableTag table = (TableTag) nodes[3];
       //temperature
       StringNode[] stringNodes = table.digupStringNode("镇江");
       StringNode name = stringNodes[0];
       w.setCity(name.toPlainTextString());
       CompositeTag td = (CompositeTag) name.getParent();
       CompositeTag tr = (CompositeTag) td.getParent();
       int columnNo = tr.findPositionOf(td);
       TableColumn nextColumn = (TableColumn) tr.childAt(5);
       Node expectedName = nextColumn.childAt(0);
       Node expectedName2 = nextColumn.childAt(2);
       //System.out.println(expectedName.getText());
       //System.out.println(expectedName2.getText());
       w.setState(expectedName.getText());
       w.setTemperature(expectedName2.getText());
       //time
       stringNodes = table.digupStringNode("时间");
       name = stringNodes[0];
       //System.out.println(name.toPlainTextString());

       String time =
        name
         .toPlainTextString()
         .substring(4, name.toPlainTextString().length())
         .trim();
       //System.out.println(time);
       w.setTime(time);
       //wind
       stringNodes = table.digupStringNode("风向");
       name = stringNodes[0];
       //System.out.println(name.toPlainTextString());

       String wind =
        name
         .toPlainTextString()
         .substring(4, name.toPlainTextString().length())
         .trim();
       //System.out.println(wind);
       w.setWind(wind);
       //wind power
       stringNodes = table.digupStringNode("风力");
       name = stringNodes[0];
       //System.out.println(name.toPlainTextString());

       String windpower =
        name
         .toPlainTextString()
         .substring(4, name.toPlainTextString().length())
         .trim();
       //System.out.println(windpower);
       w.setWindpower(windpower);

       w.setUpdateTime(System.currentTimeMillis());

      } catch (ParserException e) {

       e.printStackTrace();
      }
      return w;
     }

    解析出来的代码必须做缓存处理,

    private static long TIME_TO_LIVE = 1000 * 60 * 60 * 12;

     private Weather loadWeather() throws Exception {
      Weather weather = weather = (Weather) cache_.get("chinaweather");
      long currentTime = System.currentTimeMillis();
      if (weather != null
       && currentTime < (weather.getUpdateTime() + TIME_TO_LIVE)) {
       cache_.remove("chinaweather");
       weather = null;
      }

      if (weather == null) {
       synchronized (cache_) {
        weather = parserWeather();
        cache_.put("chinaweather", weather);
       }
      }

      return weather;
     }

    发表于 @ 2005年03月08日 14:52:00|评论(loading...)|编辑

    新一篇: JBPM的任务分派机制 | 旧一篇: SVN安装和使用(新一代的CVS)

    评论

    #SUN 发表于2006-09-13 08:40:00  IP: 211.167.43.*
    cache_.get()是那个包里面的
    #纯月 发表于2006-09-13 14:08:00  IP: 222.186.123.*
    exo的cache service,IoC得到的
    #wwwtom 发表于2007-01-16 19:18:25  IP: 219.142.132.*
    为什么必须做缓存
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 纯月