Search code examples
androidxmllarge-filestasker

Search and Parse Large XML Index from Tasker


The site: http://w1.weather.gov/xml/current_obs/index.xml; is an index of US weather stations tagged with Lat/Lon coordinates that gives current local weather observations. Of the 2257 stations in the file, each 10 line segment looks like this:

<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>

The goal is to use the android phone's GPS and Maps route information to search for the closest Latitude/Longitude match then drop down 2 lines and use that RSS link to parse and pass to a TTS service; as the phone is being automated for a 'hands free' experience while driving. From Tasker one would normally 'Get' the site which loads the global var %HTTPD and parse from there, but the file far exceeds the variable size limit. Does anyone have a suitable workaround?


Solution

  • You should use HttpURLConnection like:

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

    And then get your InputStream and parse it using SAX Parser like:

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

    Where handler is your class extending HandlerBase class.

    SAX Parser is stream-based XML parser. There're a lot of examples of SAX Parser around. Just Google them.