最近学习了xml可扩展标记语言,所以用它来实现一个天气预报的小系统。
在这里我使用的SWT编程框架来写的,这个系统是从网上获取省市区和天气状况,实时的和网络天气同步,能够查询未来5天的天气情况和和环境状况。好,下面是代码部分,有需要的,可以从我共享的资源里获取,获取地址:http://download.youkuaiyun.com/detail/qq_33220449/9617321
[天气预报下载]
(http://download.youkuaiyun.com/detail/qq_33220449/9617321)
主要界面
这里在省级选择省份,再到市级选择市或县,然后点击实时查询即可以查询到最近的天气状况。
查询县级的xml代码
public class TestCity {
public List<City> getCity(String id){
List<City> cityList=new ArrayList<City>();
try {
URL url=new URL("http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityDataset?theRegionCode="+id);
URLConnection con=url.openConnection();
con.connect();
InputStream is=con.getInputStream();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(is);
Element element=doc.getDocumentElement();
NodeList n1=doc.getElementsByTagName("City");
int len=n1.getLength();
for (int i = 0; i < len; i++) {
City city=new City();
Node node=n1.item(i);
int len2=n1.item(i).getChildNodes().getLength();
for (int j = 0; j < len2; j++) {
Node node1=n1.item(i).getChildNodes().item(j);
if(node1.getNodeType()==1){
String content=node1.getFirstChild().getNodeValue();
String nodeName=node1.getNodeName();
switch (nodeName) {
case "CityID":
city.setCityID(content);
break;
case "CityName":
city.setCityName(content);
default:
break;
}
}
}
cityList.add(city);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return cityList;
}
}
查询省级的xml代码
public class TestProvince {
public List<Province> getProvince(){
List<Province> provinceList=new ArrayList<Province>();
try {
URL url=new URL("http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionDataset");
URLConnection con=url.openConnection();
con.connect();
InputStream is=con.getInputStream();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(is);
Element element=doc.getDocumentElement();
NodeList n1=doc.getElementsByTagName("Province");
int len=n1.getLength();
for (int i = 0; i < len; i++) {
Province prov=new Province();
Node node=n1.item(i);
int len2=n1.item(i).getChildNodes().getLength();
for (int j = 0; j < len2; j++) {
Node node1=n1.item(i).getChildNodes().item(j);
if(node1.getNodeType()==1){
String content=node1.getFirstChild().getNodeValue();
String nodeName=node1.getNodeName();
switch (nodeName) {
case "RegionID":
prov.setRegionID(content);
break;
case "RegionName":
prov.setRegionName(content);
default:
break;
}
}
}
provinceList.add(prov);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return provinceList;
}
}
查询天气的xml代码
public class TestWeather {
public List<String> getWeather(String id){
List<String> weatherList=new ArrayList<String>();
try {
URL url=new URL("http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theCityCode="+id+"&theUserID=");
URLConnection con=url.openConnection();
con.connect();
InputStream is=con.getInputStream();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(is);
Element element=doc.getDocumentElement();
NodeList n1=doc.getElementsByTagName("ArrayOfString");
int len=n1.getLength();
for (int i = 0; i < len; i++) {
Node node=n1.item(i);
int len2=n1.item(i).getChildNodes().getLength();
for (int j = 0; j < len2; j++) {
Node node1=n1.item(i).getChildNodes().item(j);
if(node1.getNodeType()==1){
String content=node1.getFirstChild().getNodeValue();
String nodeName=node1.getNodeName();
if(node1.getFirstChild().getNodeValue()!=null){
weatherList.add(content);
}
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return weatherList;
}
}
这个系统可以大家可以去添加到自己的应用中,这只是我的一个练习,所以可能有些地方代码冗余或者结构不是那么好看,希望多多担待。