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.

Objectives

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

Prerequisites

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:

    python --version

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.

  1. Open the text editor and create a file named get-ap-xml.py.
  2. Save get-ap-xml.py to the desktop.
  3. Within the text editor add the following lines to get-ap-xml.py:
    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()
    
    In this snippet, we are:
    • 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

  4. 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.
  5. 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:
    cd %USERPROFILE%\Desktop
    
      If you are using OS X, type the following command into the terminal window to move to your desktop:
    cd ~/Desktop
    
  6. In the command prompt window, type the following command to execute your newly created file using Python:
    python get-ap-xml.py
    
    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.

  7. When you run the Python script, you should get an screen full of XML data returned to the terminal.
XML Command Prompt

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.