- Java code
package cn.vlabs.sso.vo.action; import javax.xml.parsers.*; import java.io.*; import java.net.URL; import java.text.DateFormat; import java.util.Date; import org.w3c.dom.*; public class GetWeather { /** * 传入选入的城市 */ public String getWeather(String cityName, String fileAddr) { // 获取google上的天气情况,写入文件 try { URL url = new URL("http://www.google.com/ig/api?hl=zh_cn&weather=" + cityName); InputStream inputstream = url.openStream(); String s, str; BufferedReader in = new BufferedReader(new InputStreamReader( inputstream)); StringBuffer stringbuffer = new StringBuffer(); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileAddr), "utf-8")); while ((s = in.readLine()) != null) { stringbuffer.append(s); } str = new String(stringbuffer); out.write(str); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } // 读取需要的数据 File file = new File(fileAddr); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); String str = null; try { DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); NodeList nodelist1 = (NodeList) doc .getElementsByTagName("forecast_conditions"); NodeList nodelist2 = nodelist1.item(0).getChildNodes(); str = nodelist2.item(4).getAttributes().item(0).getNodeValue() + ",temperature:" + nodelist2.item(1).getAttributes().item(0).getNodeValue() + "℃-" + nodelist2.item(2).getAttributes().item(0).getNodeValue() + "℃"; } catch (Exception e) { e.printStackTrace(); } return str; } public static void main(String args[]) { GetWeather ggw = new GetWeather(); String cityName = "beijing"; String fileAddr = "F:/" + cityName + ".xml"; String temperature = ggw.getWeather(cityName, fileAddr); Date nowDate = new Date(); DateFormat dateformat = DateFormat.getDateInstance(); String today = dateformat.format(nowDate); System.out.println(today + " " + cityName + "的天气情况是:" + temperature); } }
这个类main方法可以把天气预报读出来 但我的问题是怎么显示到jsp页面中呢
他是xml 具体的还请大牛指点
------解决方案--------------------
把该方法写到一servlet里,在jsp里调用