java使用dom解析google天气格式的xml文档

此段代码旨在解析类似google天气格式的xml文档,如需解析其它格式的稍做改动即可。

google天气预报xml格式如下:

 

<?xml version="1.0"?>

<xml_api_reply version="1">

<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1"

row="0" section="0">

<forecast_information>

<city data="" />

<postal_code data="" />

<latitude_e6 data="30670000" />

<longitude_e6 data="104019996" />

<forecast_date data="2011-11-16" />

<current_date_time data="2011-11-16 22:00:00 +0000" />

<unit_system data="SI" />

</forecast_information>

<current_conditions>

<condition data="多云" />

<temp_f data="59" />

<temp_c data="15" />

<humidity data="湿度: 72%" />

<icon data="/ig/images/weather/cn_cloudy.gif" />

<wind_condition data="风向: 东、风速:2 米/秒" />

</current_conditions>

<forecast_conditions>

<day_of_week data="周三" />

<low data="12" />

<high data="17" />

<icon data="/ig/images/weather/mostly_sunny.gif" />

<condition data="以晴为主" />

</forecast_conditions>

<forecast_conditions>

<day_of_week data="周四" />

<low data="11" />

<high data="18" />

<icon data="/ig/images/weather/mostly_sunny.gif" />

<condition data="以晴为主" />

</forecast_conditions>

<forecast_conditions>

<day_of_week data="周五" />

<low data="11" />

<high data="19" />

<icon data="/ig/images/weather/mostly_sunny.gif" />

<condition data="以晴为主" />

</forecast_conditions>

<forecast_conditions>

<day_of_week data="周六" />

<low data="8" />

<high data="19" />

<icon data="/ig/images/weather/mostly_sunny.gif" />

<condition data="以晴为主" />

</forecast_conditions>

</weather>

</xml_api_reply>

 

 

java解析代码如下所示:

 

package dsh.bikegis.dao.impl;

 

import java.io.FileNotFoundException;

import java.io.IOException;

 

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

 

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

 

import dsh.bikegis.dao.XmlDocument;

 

/**

 * 解析Google天氣XML文档

 * 

 * @author NanGuoCan

 * 

 */

public class WeatherParse implements XmlDocument {

 

/**

* 解析傳來的google天氣xml

* @param fileName

* 文件全路径名称

*/

@Override

public void parserXml(String fileName) {

try {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document document = db.parse(fileName);

NodeList xml=document.getChildNodes();

NodeList xml_api_reply =  xml.item(0).getChildNodes();

Node weather=xml_api_reply.item(0);

NodeList weathers=weather.getChildNodes();

//解析城市及時間預測信息

Node forecast_information=weathers.item(0);

NodeList informationNode=forecast_information.getChildNodes();

for(int i=0;i<informationNode.getLength();i++){

System.out.println(informationNode.item(i).getNodeName()+":"+informationNode.item(i).getAttributes().item(0));

}

//解析當前天氣

Node current_conditions=weathers.item(1);

NodeList currentInfo=current_conditions.getChildNodes();

for(int i=0;i<currentInfo.getLength();i++){

System.out.println(currentInfo.item(i).getNodeName()+":"+currentInfo.item(i).getAttributes().item(0));

}

//解析本周未來幾天天氣

for (int i = 2; i < weathers.getLength(); i++) {

Node forecast_conditions = weathers.item(i);

NodeList forecast_conditionsInfo = forecast_conditions.getChildNodes();

for (int j = 0; j < forecast_conditionsInfo.getLength(); j++) {

System.out.println(forecast_conditionsInfo.item(j).getNodeName()

+ ":" + forecast_conditionsInfo.item(j).getAttributes().item(0));

}

}

System.out.println("解析完毕");

} catch (FileNotFoundException e) {

System.out.println(e.getMessage());

} catch (ParserConfigurationException e) {

System.out.println(e.getMessage());

} catch (SAXException e) {

System.out.println(e.getMessage());

} catch (IOException e) {

System.out.println(e.getMessage());

}

 

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值