jsp+javaBean实现简单天气预报
getWeather.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="dataserver.*"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.io.InputStream"%>
<%@ page import="javax.xml.parsers.SAXParserFactory"%>
<%@ page import="javax.xml.parsers.SAXParser"%>
<%try{URL url = new URL("http://xml.weather.yahoo.com/forecastrss?u=c&p=CHXX0037");//此处为广州编码
InputStream input = url.openStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
weatherBean shenme=new weatherBean();
parser.parse(input,shenme);
out.println(shenme.getWeather(0)+"<br>"+shenme.getWeather(1));
}
catch (Exception ex){}
%>
<%@ page import="dataserver.*"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.io.InputStream"%>
<%@ page import="javax.xml.parsers.SAXParserFactory"%>
<%@ page import="javax.xml.parsers.SAXParser"%>
<%try{URL url = new URL("http://xml.weather.yahoo.com/forecastrss?u=c&p=CHXX0037");//此处为广州编码
InputStream input = url.openStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
weatherBean shenme=new weatherBean();
parser.parse(input,shenme);
out.println(shenme.getWeather(0)+"<br>"+shenme.getWeather(1));
}
catch (Exception ex){}
%>
-----本例jsp文件中通过实例引用获得信息, 可改为javaBean方式实现
weatherBean.java
package dataserver;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) yaoqi 2006</p>
*
* <p>Company: scut_eb</p>
*
* @author not attributable
* @version 1.0
*/
public class weatherBean extends DefaultHandler {int i=1;String weather[]={"",""};
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
String when="";
if("yweather:forecast".equals(qName)) {if(i==1){when="今日";}else{when="明日";}
String s_date = attributes.getValue(1);
Date date = null;
try {
date = new SimpleDateFormat("dd MMM yyyy", Locale.US).parse(s_date);
}
catch (Exception e) {
e.printStackTrace();
throw new SAXException("Cannot parse date: " + s_date);
}
int low = Integer.parseInt(attributes.getValue(2));
int high = Integer.parseInt(attributes.getValue(3));
String text = attributes.getValue(4);
int code = Integer.parseInt(attributes.getValue(5));
weather[i-1]=when+weather(text) + ","+wherewind(code)+low + "-" + high+"℃";
i++;
}
super.startElement(uri, localName, qName, attributes);
}
public String weather(String eng){
String chs="";
if(eng.equals("Clear")){chs="清爽无云";}
else if(eng.equals("Sunny")){chs="阳光照耀";}
else if(eng.equals("Partly Cloudy")){chs="局部有云";}
else if(eng.equals("Mostly Clear")){chs="貌似无云";}
else if(eng.equals("Mostly Sunny")){chs="点点云朵";}
else if(eng.equals("Mostly Cloudy")){chs="云彩多多";}
else if(eng.equals("Cloudy")){chs="阴云密布";}
else if(eng.equals("Sunny/Wind")){chs="晴转多云";}
else if(eng.equals("AM Clouds/PM Sun/Wind")){chs="多云转晴";}
else if(eng.equals("AM Showers")){chs="清晨小雨";}
else{chs=eng;}
return chs;}
public String wherewind(int a)
{
String wind="error";
if(a>337 && a<=360){return "北风";}
else if(a>=0 && a<=22){return "北风";}
else if(a>22 && a<=67){return "东北风";}
else if(a>67 && a<=112){return "东风";}
else if(a>112 && a<=157){return "东南风";}
else if(a>157 && a<=202){return "南风";}
else if(a>202 && a<=247){return "西南风";}
else if(a>247 && a<=292){return "西风";}
else if(a>292 && a<=337){return "西北风";}
return wind;
}
public String getWeather(int i){
return weather[i];}
}
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) yaoqi 2006</p>
*
* <p>Company: scut_eb</p>
*
* @author not attributable
* @version 1.0
*/
public class weatherBean extends DefaultHandler {int i=1;String weather[]={"",""};
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
String when="";
if("yweather:forecast".equals(qName)) {if(i==1){when="今日";}else{when="明日";}
String s_date = attributes.getValue(1);
Date date = null;
try {
date = new SimpleDateFormat("dd MMM yyyy", Locale.US).parse(s_date);
}
catch (Exception e) {
e.printStackTrace();
throw new SAXException("Cannot parse date: " + s_date);
}
int low = Integer.parseInt(attributes.getValue(2));
int high = Integer.parseInt(attributes.getValue(3));
String text = attributes.getValue(4);
int code = Integer.parseInt(attributes.getValue(5));
weather[i-1]=when+weather(text) + ","+wherewind(code)+low + "-" + high+"℃";
i++;
}
super.startElement(uri, localName, qName, attributes);
}
public String weather(String eng){
String chs="";
if(eng.equals("Clear")){chs="清爽无云";}
else if(eng.equals("Sunny")){chs="阳光照耀";}
else if(eng.equals("Partly Cloudy")){chs="局部有云";}
else if(eng.equals("Mostly Clear")){chs="貌似无云";}
else if(eng.equals("Mostly Sunny")){chs="点点云朵";}
else if(eng.equals("Mostly Cloudy")){chs="云彩多多";}
else if(eng.equals("Cloudy")){chs="阴云密布";}
else if(eng.equals("Sunny/Wind")){chs="晴转多云";}
else if(eng.equals("AM Clouds/PM Sun/Wind")){chs="多云转晴";}
else if(eng.equals("AM Showers")){chs="清晨小雨";}
else{chs=eng;}
return chs;}
public String wherewind(int a)
{
String wind="error";
if(a>337 && a<=360){return "北风";}
else if(a>=0 && a<=22){return "北风";}
else if(a>22 && a<=67){return "东北风";}
else if(a>67 && a<=112){return "东风";}
else if(a>112 && a<=157){return "东南风";}
else if(a>157 && a<=202){return "南风";}
else if(a>202 && a<=247){return "西南风";}
else if(a>247 && a<=292){return "西风";}
else if(a>292 && a<=337){return "西北风";}
return wind;
}
public String getWeather(int i){
return weather[i];}
}