I use the weather service provided by Yahoo. weather.com also provides this service when I search for it online, but I need to register it. I'll check it out, I even couldn't find the place I registered (Han's e-wen), so I was too lazy to use their house.
Yahoo's weather service address is
Http://xml.weather.yahoo.com/
Detailed explanation of the weather code in Yahoo Technical Support
Http://developer.yahoo.com/weather/
Simply put, we can send a request to Yahoo, which will return an XML file. The request address is as follows:
Http://xml.weather.yahoo.com/forecastrss? P = chxx0008 & U = C
The XML file format is as follows:
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<RSS version = "2.0" xmlns: yweather = "http://xml.weather.yahoo.com/ns/rss/1.0" xmlns: Geo = "http://www.w3.org/2003/01/geo/wgs84_pos#">
<Channel>
<Title> Yahoo! Weather-Beijing, CH </title>
<Link> Links </link>
<Description> Yahoo! Weather for Beijing, CH </description>
<Language> en-US </language>
<Lastbuilddate> Tue, 26 Dec 2006 pm CST </lastbuilddate>
<TTL> 60 </TTL>
<Yweather: Location city = "Beijing" region = "" Country = "ch"/>
<Yweather: Units temperature = "C" distance = "km" pressure = "MB" speed = "kph"/>
<Yweather: wind chill = "-7" ction = "300" speed = "32"/>
<Yweather: atmosphere humidity = "27" visibility = "999" pressure = "0" rising = "2"/>
<Yweather: astpolicmy sunrise = "7:35 AM" Sunset = "4:56 PM"/>
<Image>
<Title> Yahoo! Weather </title>
<Width> 142 </width>
<Height> 18 <Link> http://weather.yahoo.com/</link>
<URL> http://us.i1.yimg.com/us.yimg.com/ I /us/nws/th/main_142b.gif </URL>
</Image>
<Item>
<Title> conditions for Beijing, CH at 12: 00 pm CST </title>
<GEO: lat> 39.93 </GEO: lat>
<GEO: Long> 116.28 </GEO: Long>
<Link> Links </link>
<Pubdate> Tue, 26 Dec 2006 pm CST </pubdate>
<Yweather: Condition text = "fair" code = "34" temp = "0" date = "Tue, 26 Dec 2006 pm CST"/>
<Description> <! [CDATA [
<br/>
<B> current conditions: </B> <br/>
Fair, 0 C <br/>
<B> forecast: </B> <br/>
Tue-Sunny. High: 1 low:-8 <br/>
Wed-Sunny. High: 1 low:-9 <br/>
<Br/>
<A href = "Courier"> full forecast at Yahoo! Weather </a> <br/>
(Provided by the Weather Channel) <br/>
]> </Description>
<Yweather: Forecast day = "Tue" date = "26 Dec 2006" Low = "-8" high = "1" text = "Sunny" code = "32"/>
<Yweather: Forecast day = "wed" date = "27 Dec 2006" Low = "-9" high = "1" text = "Sunny" code = "32"/>
<Guid ispermalink = "false"> chxx0008_2006_12_26_12_0_cst </GUID>
</Item>
</Channel>
</RSS>
<! -- P6.weather.scd.yahoo.com uncompressed Tue Dec 26 21:04:50 PST 2006 -->
We can see the data we need. In this case, we only need to read the data in the XML operation.
I know three methods for XML operations: Dom, Sax API, and JDOM. You can use either method.
Weather = new weather ();
Vector vector = new vector ();
Yahoohandler YH = new yahoohandler ();
URL url = new URL ("http://localhost: 8080/daemon/weather. xml ");
Inputstream input = URL. openstream ();
Saxparserfactory factory = saxparserfactory. newinstance ();
Factory. setnamespaceaware (false );
Saxparser parser = factory. newsaxparser ();
Parser. parse (input, YH );
Vector = YH. getvector ();
Weather = (weather) vector. elementat (0 );
Here I have a weather. class that encapsulates some weather attributes, and a yahoohandler is used to parse data.
These statements are mainly used:
Public void startelement (string name, attributelist attributes) throws saxexception {
String temp_date;
If (name. inclusignorecase ("item ")){
Weather = new weather ();
} Else if (name. inclusignorecase ("yweather: condition ")){
Temp_date = attributes. getvalue ("date ");
System. Out. println ("pubdate is:" + temp_date );
// Weather. setpubdate ();
} Else if (name. inclusignorecase ("Description ")){
// System. Out. println ("When ther is description the temp_date is:" + temp_date );
} Else if (name. inclusignorecase ("yweather: Forecast ")){
Temp_date = attributes. getvalue ("date ");
String day = attributes. getvalue ("day ");
Int low = integer. parseint (attributes. getvalue ("low "));
Int high = integer. parseint (attributes. getvalue ("high "));
String text = attributes. getvalue ("text ");
// Convert string date to date format
If (temp_sign = 0 ){
Weather. settodayday (day );
Weather. settodaylow (low );
Weather. settodayhigh (high );
Weather. settodaytext (text );
// System. out. println ("date is:" + temp_date + "Day is:" + day + "low:" + low + "high:" + high + "text:" + text );
Temp_sign = 1;
} Else if (temp_sign = 1 ){
Weather. settomday (day );
Weather. settomlow (low );
Weather. settomhigh (high );
Weather. settomtext (text );
// System. out. println ("date is:" + temp_date + "Day is:" + day + "low:" + low + "high:" + high + "text:" + text );
Temp_sign = 0;
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/aifox/archive/2007/01/07/1476470.aspx