In a recent update (Perhaps less recent then I think) Apple had added the ability to export the data saved in the iOS Health Application. This data includes all types of data in one large XML file.
When viewed on the phone you are limited by the screen size. (There is a landscape view) But if you have several years of data you may not be able to view it all at once.
Now I was ready to throw my data in and make my web based chart. So the first thing I had to do was come up with a way to extract the weight data from the export.xml file and gather that data into a text file.
Perl6 to the rescueSo I needed to find where the weight data is located in the file, and quickly located it. The `HKQuantityTypeIdentifierBodyMass` marker is the line I was looking for. It has an startDate and value key pairs. The startDate is the date and time which corresponds to the value entry. The value is the weight in Lbs. So I now was faced with the task on developing some regular expressions to match the information I'm seeking. !(/content/images/2017/03/Screen-Shot-2017-02-28-at-11.27.28-PM.png)
The image above shows those regex's which correspond to the locations in the file I need to match. There is the recordType, which identifies a line that contains an weight entry. This is important because different apps can add an entry to the database. In my case I used a Workflow script from the lock screen and the Health app itself. Both look a little different. So if the value of recordType is
HKQuantityTypeIdentifierBodyMass I know I have a line with weight data.
dateRecord and data are the date and weight identify the date and weight information I needed. Once I found the data I need, Its time to use what I found and write/save it in a useful format.
The format needed by Google Charts line graph is:
[new Date(2014, 6, 20), 250], So now the task to to take a matching line and parse it to the format needed.
The bit of code above is nothing more then a for loop processing a text file (export.xml) and looking for the
HKQuantityTypeIdentifierBodyMass marker, then grabbing the weight and saving it a temporary variable
$weight. Finally parsing the date info I can output the results in the needed format.
Running the code from the command line like this...
...will write all the matching data to the
This was a quick and dirty project to parse the data file and get the results into a useful state. Of course there are many ways to improve the code. Perhaps using an Perl6 Grammar would be better. Instead of hardcoding the data file, I could have provided a method to provide an file path, but this was not needed for this case.
Subscribe to Independent Computing
Get the latest posts delivered right to your inbox