antonioag
Posts: 7
Joined: Mon Oct 03, 2016 7:03 pm

Parsing Weather info from XML

Mon Oct 03, 2016 7:22 pm

Hi buddies,
I'm a beginner with the Raspberry Pi. I don't know how to parse an XML code downloaded from internet and show on the terminal only whatever I want to see.

Let me explain you. I want to retrieve some temp data from a site which gives them in XML.
The code is something like that:

Code: Select all

<Weather Refresh="10">
              <locality name="Rome" lat="41,53" lon="12,28" alt="21">
              <situation temperature="23°C" temperatureF="73,4°F" flag="" humidity="73%" pression="1019 mb" wind="8 SE km/h" windKN="4,1 SE kn">
                  <description>clear sky</description>
                  <lastUpdate>20:50</lastUpdate>
             </situation>
             <ephemeris sunrise="6:55" sunset="18:39"/>
          </locality>
And here other forecast stuff which I don't care at the moment. How can I parse the code and show for example only celsius temperature, pression and humidity? Thanks everyone and sorry but this is everything new for me!

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Parsing Weather info from XML

Mon Oct 03, 2016 8:28 pm

python has built-in support for xml

https://docs.python.org/2/library/xml.dom.minidom.html

Simple example

Code: Select all

from xml.dom import minidom
xmldoc = minidom.parse('yourxmlfile.xml')
entry = xmldoc.getElementsByTagName('situation')
print entry[0].attributes['temperature'].value

antonioag
Posts: 7
Joined: Mon Oct 03, 2016 7:03 pm

Re: Parsing Weather info from XML

Mon Oct 03, 2016 9:55 pm

Wow it worked thank you!
I've added the option to automatically download the file from internet and show updated data through "urllib" and it's working fine. I've also added the "print" of "humidity" and "pression", but they belong to "situation" node, same as "temperature"

Now I have 3 more questions:
1) I've tried to show data from other node like "name" and "alt" which belong to "locality" with the command

Code: Select all

entry = xmldoc.getElementsByTagName('locality')
but it's not working.
2) How could I print something before the values. I'd like to have something like "The temperature is $value".
3) Is there any way to print values to a file where new values are added every time I run the program?

I know I may result in being boring and I'm sorry about that!

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Parsing Weather info from XML

Tue Oct 04, 2016 2:54 am

antonioag wrote:Wow it worked thank you!
I've added the option to automatically download the file from internet and show updated data through "urllib" and it's working fine. I've also added the "print" of "humidity" and "pression", but they belong to "situation" node, same as "temperature"

Now I have 3 more questions:
1) I've tried to show data from other node like "name" and "alt" which belong to "locality" with the command

Code: Select all

entry = xmldoc.getElementsByTagName('locality')
but it's not working.
2) How could I print something before the values. I'd like to have something like "The temperature is $value".
3) Is there any way to print values to a file where new values are added every time I run the program?

I know I may result in being boring and I'm sorry about that!
1) try something like

Code: Select all

entry_situation = xmldoc.getElementsByTagName('situation')
entry_locality = xmldoc.getElementsByTagName('locality')
temperature = entry_situation[0].attributes['temperature'].value
name = entry_locality[0].attributes['name'].value
2) Printing is the most basic thing in python. If you don't know how to print something then you need to learn a little python before you go any further.
https://www.tutorialspoint.com/python/index.htm
https://www.codecademy.com/learn/python

3) See above.

I'm not trying to be hard on you, but you need to learn a few things before we go any further. There is a difference between helping you and doing it for you.

antonioag
Posts: 7
Joined: Mon Oct 03, 2016 7:03 pm

Re: Parsing Weather info from XML

Tue Oct 04, 2016 6:46 pm

Thank you for your answer and...yes I know they are totally different things and I'm sorry if that is the impression which arrived to you but it is not. I've tried to do something like getting the xml from a webpage and now I figured out how to print a text before a variable. This is how my code looks now:

Code: Select all

#!/usr/bin/python

import urllib
urllib.urlretrieve ("myxmlurl", "weather.xml")

from xml.dom import minidom
xmldoc = minidom.parse('weather.xml')

entry_situation = xmldoc.getElementsByTagName('situation')
entry_locality = xmldoc.getElementsByTagName('locality')

print entry_locality[0].attributes['name'].value
print "Temperature: "+entry_situation[0].attributes['temperature'].value
print "Humidity: "+entry_situation[0].attributes['humidity'].value
print "Pression: "+entry_situation[0].attributes['pression'].value

I have still to decide if show values on a single line - I've been looking for it, I'd just need to put a "comma" at the end of every "print" section.

What I can't still get is how to save the "print" contents into a file which adds new values everytime the code is executed and how to show values from - for example "<description>clear sky</description>" node. Apparently, adapting the code by writing "entry_description" and its relative "print section" is not working.

I don't want to sound like a guy who wants everything fast, I've just told you what I achieved and what I tried.

Thank you

Return to “Python”