从 Tasker 搜索和解析大型 XML 索引

Search and Parse Large XML Index from Tasker

站点:http://w1.weather.gov/xml/current_obs/index.xml;是美国气象站的索引,标有 Lat/Lon 坐标,提供当前当地天气观测。在文件中的 2257 个站点中,每 10 个线段如下所示:

<station>
    <station_id>PADK</station_id>
    <state>AK</state>
    <station_name>Adak Island, Adak Airport</station_name>
    <latitude>51.87778</latitude>
    <longitude>-176.64583</longitude>
    <html_url>http://weather.noaa.gov/weather/current/PADK.html</html_url>
    <rss_url>http://weather.gov/xml/current_obs/PADK.rss</rss_url>
    <xml_url>http://weather.gov/xml/current_obs/PADK.xml</xml_url>
</station>

目标是使用 android phone 的 GPS 和地图路线信息搜索最近的 Latitude/Longitude 匹配然后下拉 2 行并使用该 RSS link 解析并传递给 TTS 服务;因为 phone 正在自动驾驶 'hands free' 体验。从 Tasker 中,通常 'Get' 加载全局变量 %HTTPD 并从那里解析的站点,但文件远远超过了变量大小限制。有人有合适的解决方法吗?

你应该像这样使用 HttpURLConnection

URL url = new URL("http://w1.weather.gov/xml/current_obs/index.xml");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

然后获取您的 InputStream 并使用 SAX 解析器对其进行解析,例如:

 // get a new SAXParser
 SAXParser parser = factory.newSAXParser();
 // parse the stream
 parser.parse(is, handler);

其中 handler 是您的 class 扩展 HandlerBase class。

SAX 解析器是基于流的 XML 解析器。周围有很多 SAX 解析器的示例。只有 Google 他们。