首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

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

2012-12-27 
java使用dom解析google天气格式的xml文档此段代码旨在解析类似google天气格式的xml文档,如需解析其它格式

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());

}

?

}

}


热点排行