这两天一直在弄天气信息的问题,不知道为什么百度别人都写的超级复杂,看的头都大了,总算是写出来了。。。
我用的是SSH框架,所以直接就有dom4j的架包,如果没有可以下一个
String[] dictionaryStrings = { "龙卷风", "热带风暴", "飓风", "强雷阵雨", "雷阵雨", "小雨加雪", "雨加冰雹", "雪加冰雹", "冰雨", "毛毛雨", "冻雨", "阵雨", "阵雨", "小雪", "零星小雪", "高吹雪", "雪", "冰雹", "雨夹雪", "尘", "雾", "薄雾", "多烟的", "大风", "有风", "寒冷", "阴天", "夜间阴天", "白天阴天", "夜间多云", "白天多云", "夜间清亮", "晴朗", "转晴", "转晴", "雨夹冰雹", "热", "雷阵雨", "雷阵雨", "雷阵雨", "雷阵雨", "大雪", "阵雪", "大雪", "多云", "雷", "阵雪", "雷雨" }; URL url=null; try { url=new URL("http://weather.yahooapis.com/forecastrss?w=2502265&u=c");//w是城市的代码,u是温度单位 } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } SAXReader saxReader = new SAXReader(); List code=new ArrayList(); try { Document document = saxReader.read(url); Element employees=document.getRootElement(); for(Iterator i = employees.elementIterator(); i.hasNext();){ Element employee = (Element) i.next(); for(Iterator j = employee.elementIterator(); j.hasNext();){ Element node=(Element) j.next(); if(node.getName().equals("item")){ for(Iterator k = node.elementIterator(); k.hasNext();){ Element val=(Element) k.next(); if(val.getName().equals("forecast")){ code.add(val.attributeValue("code")); } } } } } } catch (DocumentException e) { System.out.println(e.getMessage()); } System.out.println(code.get(0)); int r=Integer.valueOf(code.get(0).toString()); System.out.println(dictionaryStrings[r]);
多简单的东西啊!!!