天气预报系统——XML应用

本文介绍了一个基于XML的天气预报系统,采用SWT编程框架,实现了从网络获取并显示实时天气信息的功能,允许查询未来5天的天气和环境状况。用户可按省市区逐级选择查询,代码资源可在优快云下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近学习了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;
    }
}

这个系统可以大家可以去添加到自己的应用中,这只是我的一个练习,所以可能有些地方代码冗余或者结构不是那么好看,希望多多担待。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值