Parsing XML weather data

The Python standard library contains an xml package, which consists of several submodules for parsing or creating XML data. The xml.etree.ElementTree submodule is a simple, lightweight parser that should meet our needs.

Let's import ElementTree into our network.py file as follows:

from xml.etree import ElementTree

Now, back at the end of our function, we'll parse the XML data in our response object as follows:

    xmlroot = ElementTree.fromstring(response.read())

The fromstring() method takes an XML string and returns an Element object. To get at the data we need, we'll need to understand what an Element object represents, and how to work with it.

XML is a hierarchical representation of data; an element represents a ...

Get Python GUI Programming - A Complete Reference Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.