konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks
          首先,我們新建一個類,存放天氣信息

          /*
          ?*?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;
          ?}


          posted on 2007-03-08 06:56 konhon 優華 閱讀(2227) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 宁安市| 报价| 拉孜县| 萨迦县| 吴忠市| 南昌市| 昆山市| 郴州市| 鹤岗市| 本溪市| 通化市| 石柱| 巧家县| 达州市| 五原县| 叶城县| 乐业县| 呼和浩特市| 平泉县| 六枝特区| 丹东市| 澄迈县| 马公市| 含山县| 龙门县| 迁安市| 瑞金市| 游戏| 固安县| 沧州市| 银川市| 高碑店市| 栾川县| 泰和县| 福清市| 乌兰察布市| 天门市| 大关县| 大田县| 准格尔旗| 永平县|