gpt4 book ai didi

java - xml使用sax解析器从子标签解析数据

转载 作者:行者123 更新时间:2023-11-29 05:49:27 25 4
gpt4 key购买 nike

我是 android 的新手,所以如果我问的是一些愚蠢的问题,我很抱歉。我正在使用 android 中的天气解析。我正在使用 SAX 解析器。现在,我坚持使用内容处理程序。通过这么多教程,我有了解析数据的想法,但所有这些示例都基于确实包含标签中属性的 XML 文件。对于我从中检索数据的 XML 文件,他的标签中没有属性,只有子节点。现在我被困在这里我不知道如何获取子节点的值。

   <data>
<request>
<type>City</type>
<query>Peshawar, Pakistan</query>
</request>
<current_condition>
<observation_time>01:04 PM</observation_time>
<temp_C>20</temp_C>
<temp_F>68</temp_F>
<weatherCode>113</weatherCode>
<weatherIconUrl>
</weatherIconUrl>

现在我想从“”和“”中获取数据,但我真的找不到它,请帮助我。这是我的内容处理程序类代码。

public class HandlingXmlStuff extends DefaultHandler {

XmlDataCollected info=new XmlDataCollected();

public String getInformation() {
return info.dataToString();
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub


if(localName.equals("query")) {
String city=....???;
info.setCity(city);
} else if(localName.equals("temp_f")) {
String t=...???;
int temp=Integer.parseInt(t);
info.settemp(temp);
}
}
}

最佳答案

这是从 xml data from the url 中提取值的完整解析解决方案.

来自 provided link 的示例 xml 数据:

<?xml version="1.0" encoding="UTF-8"?>
<data>
<request>
<type>City</type>
<query>Peshawar, Pakistan</query>
</request>
<current_condition>
<observation_time>04:31 AM</observation_time>
<temp_C>7</temp_C>
<temp_F>45</temp_F>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]>
</weatherDesc>
<windspeedMiles>0</windspeedMiles>
<windspeedKmph>0</windspeedKmph>
<winddirDegree>0</winddirDegree>
<winddir16Point>N</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>70</humidity>
<visibility>4</visibility>
<pressure>1021</pressure>
<cloudcover>0</cloudcover>
</current_condition>
<weather>
<date>2013-01-26</date>
<tempMaxC>22</tempMaxC>
<tempMaxF>71</tempMaxF>
<tempMinC>7</tempMinC>
<tempMinF>45</tempMinF>
<windspeedMiles>8</windspeedMiles>
<windspeedKmph>13</windspeedKmph>
<winddirection>NW</winddirection>
<winddir16Point>NW</winddir16Point>
<winddirDegree>315</winddirDegree>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]>
</weatherDesc>
<precipMM>0.0</precipMM>
</weather>
<weather>
<date>2013-01-27</date>
<tempMaxC>20</tempMaxC>
<tempMaxF>68</tempMaxF>
<tempMinC>8</tempMinC>
<tempMinF>46</tempMinF>
<windspeedMiles>7</windspeedMiles>
<windspeedKmph>12</windspeedKmph>
<winddirection>N</winddirection>
<winddir16Point>N</winddir16Point>
<winddirDegree>352</winddirDegree>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]>
</weatherDesc>
<precipMM>0.0</precipMM>
</weather>
<weather>
<date>2013-01-28</date>
<tempMaxC>20</tempMaxC>
<tempMaxF>68</tempMaxF>
<tempMinC>7</tempMinC>
<tempMinF>45</tempMinF>
<windspeedMiles>9</windspeedMiles>
<windspeedKmph>14</windspeedKmph>
<winddirection>NW</winddirection>
<winddir16Point>NW</winddir16Point>
<winddirDegree>316</winddirDegree>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]>
</weatherDesc>
<precipMM>0.0</precipMM>
</weather>
<weather>
<date>2013-01-29</date>
<tempMaxC>21</tempMaxC>
<tempMaxF>70</tempMaxF>
<tempMinC>8</tempMinC>
<tempMinF>46</tempMinF>
<windspeedMiles>6</windspeedMiles>
<windspeedKmph>9</windspeedKmph>
<winddirection>NW</winddirection>
<winddir16Point>NW</winddir16Point>
<winddirDegree>306</winddirDegree>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Sunny]]>
</weatherDesc>
<precipMM>0.0</precipMM>
</weather>
<weather>
<date>2013-01-30</date>
<tempMaxC>19</tempMaxC>
<tempMaxF>66</tempMaxF>
<tempMinC>8</tempMinC>
<tempMinF>47</tempMinF>
<windspeedMiles>8</windspeedMiles>
<windspeedKmph>13</windspeedKmph>
<winddirection>WNW</winddirection>
<winddir16Point>WNW</winddir16Point>
<winddirDegree>289</winddirDegree>
<weatherCode>116</weatherCode>
<weatherIconUrl><![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Partly Cloudy ]]>
</weatherDesc>
<precipMM>0.0</precipMM>
</weather>
</data>

HandlingXmlStuff.java:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
*
* @author visruth
*/
public class HandlingXmlStuff extends DefaultHandler {

private String key;
private Map<String, String> request = new HashMap<String, String>();
private Map<String, String> current_condition = new HashMap<String, String>();
private List<Map<String, String>> weather = new ArrayList<Map<String, String>>();
private Map<String, String> weatherMap;
private boolean requestStatus;
private boolean current_conditionStatus;
private boolean weatherStatus;

public Map<String, String> getCurrent_condition() {
return this.current_condition;
}

public Map<String, String> getRequest() {
return this.request;
}

public List<Map<String, String>> getWeather() {
return this.weather;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
key = qName;
if (qName.equals("request")) {

requestStatus = true;
} else if (qName.equals("current_condition")) {

current_conditionStatus = true;
} else if (qName.equals("weather")) {
weatherMap = new HashMap<String, String>();
weatherStatus = true;
}

}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

String value = new String(ch, start, length).trim();
if (requestStatus) {
if (!key.trim().equals("request") && !key.trim().isEmpty() && !value.isEmpty()) {
request.put(key, value);
}
} else if (current_conditionStatus) {
if (!key.trim().equals("current_condition") && !key.trim().isEmpty() && !value.isEmpty()) {
current_condition.put(key, new String(ch, start, length).trim());
}
} else if (weatherStatus) {
if (!key.trim().equals("weather") && !key.trim().isEmpty() && !value.isEmpty()) {
weatherMap.put(key, new String(ch, start, length).trim());
}
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

if (qName.equals("request")) {
requestStatus = false;
} else if (qName.equals("current_condition")) {
current_conditionStatus = false;
} else if (qName.equals("weather")) {
weather.add(weatherMap);
weatherStatus = false;
}
}

public void parseDocument() {
//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
//get a new instance of parser
SAXParser sp = spf.newSAXParser();
//parse the file and also register this class for call backs
sp.parse("http://free.worldweatheronline.com/feed/weather.ashx?q=peshawar,pakistan&format=xml&num_of_days=5&key=eab9f57359164426132301", this);
} catch (SAXException se) {
se.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}

您可以使用以下代码检索值:

HandlingXmlStuff handlingXmlStuf = new HandlingXmlStuff();
handlingXmlStuf.parseDocument();
System.out.println("----values inside request tag-----");
Map<String, String> request = handlingXmlStuf.getRequest();
for (String key : request.keySet()) {
System.out.println(key + ":" + request.get(key) + ":" + key);
}
System.out.println("----values inside current_condition tag-----");
Map<String, String> current_condition = handlingXmlStuf.getCurrent_condition();
for (String key : current_condition.keySet()) {
System.out.println(key + ":" + current_condition.get(key) + ":" + key);
}

List<Map<String, String>> weather = handlingXmlStuf.getWeather();
for (Map<String, String> map : weather) {
System.out.println("\n----each set of weather-----start");
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key) + ":" + key);
}
System.out.println("----each set of weather-----end");
}

从上面的示例 xml 数据中,您将获得以下输出:

----values inside request tag-----
query:Peshawar, Pakistan:query
type:City:type
----values inside current_condition tag-----
cloudcover:0:cloudcover
observation_time:04:31 AM:observation_time
pressure:1021:pressure
temp_C:7:temp_C
visibility:4:visibility
temp_F:45:temp_F
windspeedMiles:0:windspeedMiles
precipMM:0.0:precipMM
winddirDegree:0:winddirDegree
winddir16Point:N:winddir16Point
weatherIconUrl:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png:weatherIconUrl
humidity:70:humidity
windspeedKmph:0:windspeedKmph
weatherCode:113:weatherCode
weatherDesc:Sunny:weatherDesc

----each set of weather-----start
windspeedMiles:8:windspeedMiles
winddirection:NW:winddirection
date:2013-01-26:date
precipMM:0.0:precipMM
winddir16Point:NW:winddir16Point
winddirDegree:315:winddirDegree
weatherIconUrl:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png:weatherIconUrl
tempMinC:7:tempMinC
windspeedKmph:13:windspeedKmph
tempMaxC:22:tempMaxC
weatherCode:113:weatherCode
tempMaxF:71:tempMaxF
tempMinF:45:tempMinF
weatherDesc:Sunny:weatherDesc
----each set of weather-----end

----each set of weather-----start
windspeedMiles:7:windspeedMiles
winddirection:N:winddirection
date:2013-01-27:date
precipMM:0.0:precipMM
winddir16Point:N:winddir16Point
winddirDegree:352:winddirDegree
weatherIconUrl:_0001_sunny.png:weatherIconUrl
tempMinC:8:tempMinC
windspeedKmph:12:windspeedKmph
tempMaxC:20:tempMaxC
weatherCode:113:weatherCode
tempMaxF:68:tempMaxF
tempMinF:46:tempMinF
weatherDesc:Sunny:weatherDesc
----each set of weather-----end

----each set of weather-----start
windspeedMiles:9:windspeedMiles
winddirection:NW:winddirection
date:2013-01-28:date
precipMM:0.0:precipMM
winddir16Point:NW:winddir16Point
winddirDegree:316:winddirDegree
weatherIconUrl:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png:weatherIconUrl
tempMinC:7:tempMinC
windspeedKmph:14:windspeedKmph
tempMaxC:20:tempMaxC
weatherCode:113:weatherCode
tempMaxF:68:tempMaxF
tempMinF:45:tempMinF
weatherDesc:Sunny:weatherDesc
----each set of weather-----end

----each set of weather-----start
windspeedMiles:6:windspeedMiles
winddirection:NW:winddirection
date:2013-01-29:date
precipMM:0.0:precipMM
winddir16Point:NW:winddir16Point
winddirDegree:306:winddirDegree
weatherIconUrl:http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png:weatherIconUrl
tempMinC:8:tempMinC
windspeedKmph:9:windspeedKmph
tempMaxC:21:tempMaxC
weatherCode:113:weatherCode
tempMaxF:70:tempMaxF
tempMinF:46:tempMinF
weatherDesc:Sunny:weatherDesc
----each set of weather-----end

----each set of weather-----start
windspeedMiles:8:windspeedMiles
winddirection:WNW:winddirection
date:2013-01-30:date
precipMM:0.0:precipMM
winddir16Point:WNW:winddir16Point
winddirDegree:289:winddirDegree
weatherIconUrl:unny_intervals.png:weatherIconUrl
tempMinC:8:tempMinC
windspeedKmph:13:windspeedKmph
tempMaxC:19:tempMaxC
weatherCode:116:weatherCode
tempMaxF:66:tempMaxF
tempMinF:47:tempMinF
weatherDesc:Partly Cloudy:weatherDesc
----each set of weather-----end

startElement 在读取开始标记时调用,endElement 在读取结束标记时调用,characters 方法每次都调用在 startElementendElement 方法调用之后。

关于java - xml使用sax解析器从子标签解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com