Coding 201: Parsing XML using Python
In this Learning Lab you will learn the basics of parsing XML content using Python. We will use the CMX Mobility Services as a data source and query for the names of the available access points and put them into a simple list for display. If you want to dig deeper into CMX, review the Mobility Services Engine (MSE) Learning Lab.
Completion Time: 20 minutes
- Understand the basics of reading and parsing XML content via HTTP using Python
- Evaluate data returned from the CMX Mobility Service API
- Learn how to use Python to extract only the XML data you want using Minidom
You will want to make sure you have gone through the Coding 101 lab if you are unfamiliar with Python and retrieving results from a RESTful service.
You should also have a basic familiarity with XML. If you do not, consider visiting the W3Schools XML Tutorial to get a firm base to build upon.
For this lab we will be using Python 3.4+. If you are on a DevNet Zone station, the correct version of Python should already be installed. If you are following this lab online, you can verify your version run the following command at a terminal or command prompt window:
Step 1. Make a HTTP REST call with Python
To get started, create a simple Python script that can make a HTTP request to the CMX MSE Server.
- Open the text editor and create a file named get-ap-xml.py.
- Save get-ap-xml.py to the desktop.
- Within the text editor add the following lines to get-ap-xml.py:
In this snippet, we are:
from urllib.request import Request, urlopen req = Request('https://msesandbox.cisco.com/api/contextaware/v1/maps/info/DevNetCampus/DevNetBuilding/DevNetZone') req.add_header('Authorization', 'Basic bGVhcm5pbmc6bGVhcm5pbmc==') response = urlopen(req) responseString = response.read().decode("utf-8") print(responseString) response.close()
- Importing the required Request and urlopen libraries
- Constructing a request to the CMX URI
- Adding Basic authentication to our Request
- Opening the request and getting a Response back from the CMX URI
- Parsing the Response as a String and printing it out to the console
- Save the get-ap-xml.py file. If you want to just download or review the current code, you can get it from GitHub here.
- Open a command prompt.
If your are using the Windows OS, type the following command into the command prompt window to move to your desktop:
If you are using OS X, type the following command into the terminal window to move to your desktop:
- In the command prompt window, type the following command to execute your newly created file using Python:
Note: You may need to run the python command as python3.4 get-ap-xml.py on OS X to force using the 3.4 version of Python. If you get any errors, verify the version of python being executed and double-check the code.
- When you run the Python script, you should get an screen full of XML data returned to the terminal.
By default, CMX will return the data in XML. For this first step, this is what we want. Let's clean up this data parse out something more specific in the next step.