Parsing XML in Python


Since a lot of API responses are in XML, we'll review XML parsing in Python to help you get to the interesting data faster.

This tutorial assumes that you've already gone through our Python getting started tutorial and are familiar with how our Python SDK works. It requires Python 2.7 or later.

Get an XML Response

1 Log in to Temboo and go to the Yahoo > Weather > GetWeatherByAddress Choreo in our Library. Select Python from the drop down menu at the top of the page.

2 Enter a location in the Address input and click Generate Code to test the Choreo from our website.

3 You get a whole bunch of XML in the Response output with lots of weather-related information about the location. Next we'll see how to parse through this response in Python and pick out only the pieces we're interested in.

Parse it in Python

4 Create a new .py file and copy in the code below. Steps 5 & 6 talk you through what happens in the code.

from xml.etree import ElementTree

from temboo.Library.Yahoo.Weather import GetWeatherByAddress
from temboo.core.session import TembooSession

# Create a new Temboo session
# Replace with your own Temboo Account Name, Application Name, and Application Key
session = TembooSession('ACCOUNT_NAME', 'APP_NAME', 'APP_KEY')

# Instantiate the choreo, using the session object
getWeatherByAddressChoreo = GetWeatherByAddress(session)

# Get an InputSet object for the choreo
getWeatherByAddressInputs = getWeatherByAddressChoreo.new_input_set()

# Set inputs
getWeatherByAddressInputs.set_Address("104 Franklin St, New York, NY")

# Execute choreo
getWeatherByAddressResults = getWeatherByAddressChoreo.execute_with_results(getWeatherByAddressInputs)

# Convert the XML response to an ElementTree object
tree = ElementTree.fromstring(getWeatherByAddressResults.get_Response())

# With ElementTree, you need to specify the namespaces
# This is the appropriate namespace for the yweather:condition element
namespaces = {'yweather': 'http://xml.weather.yahoo.com/ns/rss/1.0'}

condition = tree.find("channel/item/yweather:condition", namespaces=namespaces)

# Parse the temp and text attributes
temp = condition.attrib["temp"]
text = condition.attrib["text"]

# Print the temperature and description text
print (temp)
print (text)

5 First we have to convert the XML response from Yahoo to an ElementTree object.

6 Next we need to parse out the data we want from the XML file. It helps to look at the XML file's structure to get an idea of how it is organized. We want the text and temp attributes. You can find them in:
<channel><item><yweather:condition>.

7That's it. You can run the code to try it out. You should see the weather condition text and temp printed in the console.

What next?

Now you're ready to tackle all sorts of XML parsing tasks. You can find tons of Choreos that return XML in the Library.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.

We're hiring!

Like what we do? Take a look at our open positions.


Back