XML Parsing with Python

Have you ever checked Health App on your iPhone? Have you ever wondered how to get data from that app? Have you ever thought about making a nice Tableau dashboard out of it? I have, so I did.

Apple Health iPhone App

Getting your data is easy, go to your Health App > Health Data > Profile > Export Health Data. You’ll have apple_health_export.zip with two xml files: export.xml and export_cda.xml. export_cda.xml stores all your personal details you don’t want to share. We’re interested in export.xml here. Depending on what apps or devices you use (watch, band, scale, app), your data – especially their depth – can differ a bit.
About two years ago, I got a FitBit to track my steps. My goal is 10k a day – I’ve read it’s the right amount that makes you fit and healthy (I’ve also read 10k is either too much or not enough). There are days I reach that number and days I’m too lazy. FitBit app is pretty neat and shows you quite nice stats. Also, they send you a weekly report via e-mail. Sweet! In the last couple of weeks, I forgot to wear my FitBit. No weekly reports, no stars on my wrist when I make the 10000th step. Well, I thought, my iPhone tracks the steps as well, how about Health stats? Meh. Not as good. That’s why I thought about visualisating those data myself.

To enjoy this vizz properly, use the enlarging button at the very right bottom end.

iPhone Fitbit app daily overview
Fitbit weekly report via email

The obvious difference is that iPhone tracks only the moments it’s with you either in your hand, a pocket or a bag. That means every trip to a bathroom without your inseparable little friend or every walk of shame with a dead taxi-calling device is not there. Makes sense, right? Anyway, every step you made with the same Apple ID is there. That means I have some kind of data from the very first day with my very first iPhone! Exciting! Kind of.

As I don’t want to share any real (or currently relevant) data here #sorrynotsorry #dataprivacyfirst I altered a bit of data just to give you an idea what it looks like.

First, I had to understand how Python dictionaries work and how to iterate those. Not exactly rocket science. Well – just in case you’re new to Python as I am, check these amazing MIT courses on YouTube (MIT 6.0001 Introduction to Computer Science and Programming in Python)
Why do you need to know dictionary and its structure? An XML file is a machine- and human-readable data file with tags that work as keys in dictionary. It’s as simple as that.

This script is pretty straightforward and simple. I used xmltodict library to deal with parsing my XML file and there’s not much else needed.

As I’m not able to make code snippets work in here, you can find the script as well as mock data on my GitHub. Happy parsing!

Leave a Comment

Your email address will not be published. Required fields are marked *