Tech Blog

How to parse the xml object returned from analytics API with Python

Following is a high level workflow for using Alma API, and parsing the response in Python XML Element Tree:

  • Create a report in Analytics
  • Go into your institutional developer’s portal and set up an API Key for Analytics. See
  • Create a Python script that queries this report through the API.   The URL should be constructed as follows: /almaws/v1/analytics/reports?apikey=<api key>&limit=1000&path=<path to Analytics report, from address bar>
  • This API returns only XML.  To parse the XML in Python, you can use Element Tree.  Python doesn’t parse XML natively, so you need something.  Include this import line: import xml.etree.cElementTree as et
  • Iterate through results with Python XML Element Tree.

Following is a code snippet for how to iterate through the results:


import xml.etree.cElementTree as et

report = requests.get(URL)

# print("\n\nReport: \n" + json.dumps(report) + "\n")
# print ("\n\n\n\n" + str(report.content) + "\n\n\n\n")

report_string = report.content
tree = et.ElementTree(et.fromstring(report.content))
root = tree.getroot()

isFinished = root[0][1].text
resumptionToken = root[0][0].text
isFinishedContinue = isFinished
result_set = root[0][2][0]

while isFinishedContinue == "false":
    full_path = url + "&token=" + resumptionToken
    reportContinue = requests.get(url + "&token=" + resumptionToken)
    # print ("\n\n\n\n" + str(reportContinue.content) + "\n\n\n\n")
    report_string += reportContinue.content

    treeContinue = et.ElementTree(et.fromstring(reportContinue.content))
    rootContinue = treeContinue.getroot()
    result_set_continue = rootContinue[0][1][0]

    isFinishedContinue = rootContinue[0][0].text

Leave a Reply