Skip to content

Instantly share code, notes, and snippets.

@HerbCSO
Created April 28, 2012 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HerbCSO/2519671 to your computer and use it in GitHub Desktop.
Save HerbCSO/2519671 to your computer and use it in GitHub Desktop.
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