Parse lengthy XML from weathermap


#1

Hey guys,

So I’m working on trying to parse data from a weathermap xml. I’m sure for someone who parses this doesn’t seem to be a difficult task but I’m having a bit of trouble. This is the xml data I’m working with:

<weatherdata>
<location>
<name>Minneapolis</name>
<type/>
<country>US</country>
<timezone/>
<location altitude="0" latitude="45.1033" longitude="-93.6914" geobase="geonames" geobaseid="420019282"/>
</location>
<credit/>
<meta>
<lastupdate/>
<calctime>0.005</calctime>
<nextupdate/>
</meta>
<sun rise="2018-04-14T11:29:58" set="2018-04-15T01:00:23"/>
<forecast>
<time from="2018-04-14T21:00:00" to="2018-04-15T00:00:00">
<symbol number="601" name="snow" var="13n"/>
<precipitation unit="3h" value="4.9991" type="snow"/>
<windDirection deg="51.0021" code="NE" name="NorthEast"/>
<windSpeed mps="7.25" name="Moderate breeze"/>
<temperature unit="kelvin" value="269.03" min="269.03" max="271.479"/>
<pressure unit="hPa" value="1000.74"/>
<humidity value="86" unit="%"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>
<time from="2018-04-15T00:00:00" to="2018-04-15T03:00:00">
<symbol number="600" name="light snow" var="13n"/>
<precipitation unit="3h" value="0.531" type="snow"/>
<windDirection deg="51.0013" code="NE" name="NorthEast"/>
<windSpeed mps="7.26" name="Moderate breeze"/>
<temperature unit="kelvin" value="268.52" min="268.52" max="270.363"/>
<pressure unit="hPa" value="1001.53"/>
<humidity value="87" unit="%"/>
<clouds value="overcast clouds" all="88" unit="%"/>
</time>
<time from="2018-04-15T03:00:00" to="2018-04-15T06:00:00">
<symbol number="600" name="light snow" var="13n"/>
<precipitation unit="3h" value="0.3493" type="snow"/>
<windDirection deg="47.5001" code="NE" name="NorthEast"/>
<windSpeed mps="6.26" name="Moderate breeze"/>
<temperature unit="kelvin" value="268.2" min="268.2" max="269.427"/>
<pressure unit="hPa" value="1001.51"/>
<humidity value="83" unit="%"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>
<time from="2018-04-15T06:00:00" to="2018-04-15T09:00:00">
**<symbol number="600" name="light snow" var="13n"/>**
<precipitation unit="3h" value="0.3727" type="snow"/>
<windDirection deg="41.0072" code="NE" name="NorthEast"/>
<windSpeed mps="5.46" name=""/>
<temperature unit="kelvin" value="268.35" min="268.35" max="268.959"/>
<pressure unit="hPa" value="1000.78"/>
<humidity value="82" unit="%"/>
<clouds value="overcast clouds" all="92" unit="%"/>
</time>

What I would like to do is have a function that can read through this data and output the 3 digit int that follows the <symbol number=“XXX” name = in all four places. Could someone at least get me started? I’ve read through plenty of pages on parsing xmls but this isn’t really my forte… Any help is appreciated. Thanks,


#2

I suspect that you don’t really need to parse the XML in order to extract just a couple of fields. Simple string matching should be sufficient. You know that the interesting bits of the XML always start with a “<symbol number=” prefix.


#3

How are you receiving the data?
Does the service provide JSON data too?
If so, you could use the cloud webhook parsing feature to reduce the output to the bare minimum you are interested in and use a JSON parser on the device to further extract that.