Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse Garmin TCX file
#!/usr/bin/env ruby
require 'nokogiri'
xml=Nokogiri::XML File.open 'My Activities_history.tcx'
xml.remove_namespaces!
$stdout.reopen("activities.csv", "w")
$stdout.sync = true
$stderr.reopen($stdout)
puts '"Sport","Id","LapStartTime","TotalTimeSeconds","DistanceMeters","MaximumSpeed","Calories","AverageHeartRateBpm","MaximumHeartRateBpm"'
xml.xpath("//Activity").each do |activity|
activity.xpath('Lap').each do |lap|
puts "\"#{ activity['Sport'] }\","\
"\"#{ activity.xpath('Id').children }\","\
"\"#{ lap['StartTime'] }\","\
"#{ lap.xpath('TotalTimeSeconds').children },"\
"#{ lap.xpath('DistanceMeters').children },"\
"#{ lap.xpath('MaximumSpeed').children },"\
"#{ lap.xpath('Calories').children },"\
"#{ lap.xpath('AverageHeartRateBpm/Value').children },"\
"#{ lap.xpath('MaximumHeartRateBpm/Value').children }"\
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment