DOM是用与平台无关和语言无关的方式表示XML文档的官方W3C标准,DOM是以层次结构组织的节点或信息片段的集合。DOM是基于树的,DOM相对SAX来说简单,耗内存...
本次学习目标:了解DOM解析XML ,并用DOM解析谷歌提供的天气
谷歌提供的天气接口是 http://www.google.com/ig/api?hl=zh_CN&weather=wuhan 这个接口末尾是wuhan 即 "武汉" 的拼音,依次类推,北京的查询方式是把后面拼音换成beijing就行了,这个接口是查询武汉四天的天气。
根元素(Element)是 xml_api_reply 即树的根 然后往里面扩展。
我要获取节点forecas_conditions中的数据
DOM初始工作需要几个函数
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new InputStreamReader(lianJie(strUrl) )));
然后通过Document对象解析XML,解析XML时会用到节点,并取得他的值 用到类 NodeList ,Node. 下面开始上我的程序
package com.study.weather;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Weather
{
public InputStream lianJie(String strUrl) throws IOException
{
URL url = new URL(strUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream is = urlConnection.getInputStream();
if(is!=null)
{
return is;
}
return null;
}
public void resolutionXML(String strUrl) throws ParserConfigurationException, SAXException, IOException
{
WeatherData wd = new WeatherData();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new InputStreamReader(lianJie(strUrl) )));
// 得到xml_api_reply
Element rootElement = document.getDocumentElement();
System.out.println(rootElement.getNodeName());
Node weatherNode = rootElement.getElementsByTagName("weather").item(0);
// Node weatherNode = rootElement.getFirstChild();
System.out.println(weatherNode.getNodeName());
// 得到weather
// Node nodeWeather = weatherNode.getChildNodes();
// 得到weather下节点数组
NodeList nodeForecastWeathers = weatherNode.getChildNodes();
// 遍历weather下的节点
for(int i=0; i<nodeForecastWeathers.getLength(); i++)
{
Node nodeForecastWeather = nodeForecastWeathers.item(i);
// 筛选节点 找名称为 forecast_conditions 节点
if(nodeForecastWeather.getNodeType()==Node.ELEMENT_NODE
&&nodeForecastWeather.getNodeName().equals("forecast_conditions"))
{
// 建立forecast_conditions下节点数组
NodeList nodeListForecastConditions = nodeForecastWeather.getChildNodes();
for(int j=0; j<nodeListForecastConditions.getLength(); j++)
{
//day_of_week low high condition
Node data = nodeListForecastConditions.item(j);
if(data.getNodeName().equals("day_of_week"))
{
wd.setDayOfWeek(data.getAttributes().getNamedItem("data").getNodeValue());
}
else if(data.getNodeName().equals("low"))
{
wd.setLow(data.getAttributes().item(0).getNodeValue());
}
else if(data.getNodeName().equals("high"))
{
wd.setHigh(data.getAttributes().item(0).getNodeValue());
}
else if(data.getNodeName().equals("condition"))
{
wd.setConditionData(data.getAttributes().item(0).getNodeValue());
}
}
System.out.println(wd.printWeatheaInfo());
}
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
Weather weather = new Weather();
weather.resolutionXML("http://www.google.com/ig/api?hl=zh_CN&weather=wuhan");
}
class WeatherData
{
String dayOfWeek;
String low;
String high;
String conditionData;
public void setDayOfWeek(String dayOfWeek)
{
this.dayOfWeek = dayOfWeek;
}
public void setLow(String low)
{
this.low = low;
}
public void setHigh(String high)
{
this.high = high;
}
public void setConditionData(String conditionData)
{
this.conditionData = conditionData;
}
public String printWeatheaInfo()
{
return dayOfWeek+"\n"+"温度: "+low+"~~"+high+" \n天气情况: "+conditionData;
}
}
}
这个是执行结果,完全解析正确
PS:多看,多读,多写代码