-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAppleHealthXmlParser.py
33 lines (26 loc) · 1.01 KB
/
AppleHealthXmlParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import xml.sax
from Record import Record
supportedTypes = ["HKQuantityTypeIdentifierBodyMass", "HKQuantityTypeIdentifierStepCount",
"HKQuantityTypeIdentifierDistanceWalkingRunning"]
records = []
class AppleHealthExport(xml.sax.ContentHandler):
def startElement(self, name, attrs):
if name == "Record":
for (k, v) in attrs.items():
if k == 'type':
recordType = v
if k == 'startDate':
startTime = v
if k == 'endDate':
endTime = v
if k == 'value':
value = v
if recordType in supportedTypes:
record = Record(recordType, startTime, endTime, value)
records.append(record)
def parse(filename):
print "Reading the XML file... This might take some time..."
parser = xml.sax.make_parser()
parser.setContentHandler(AppleHealthExport())
parser.parse(open(filename, "r"))
return records