Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difficulty parsing gpx file with libxml-ruby

Status
Not open for further replies.

mrampton

Programmer
Jan 11, 2001
7
US
Hey guys,

I'm hoping that someone with more xml experience can see the easy explanation as to why i'm having difficulties here. Basically I'm attempting to extract elements from a gpx file (xml file for gps data). I had this working in rexml but it was TERRIBLY slow.

And, I can actually get it working if I muck with the gpx file and delete some of the urls in the header (for the xmlns variables).

Anyway, here's my model code and an example of the gpx file that it won't parse. No error messages appear -- it seems to simply skip right over the loop that cycles through the xml file.

Code:
require 'rubygems'
require 'xml/libxml'

class Path < ActiveRecord::Base
    has_many :geocodes
    belongs_to :trail

    def readGPX(file)
       
    if File.exist?("#{RAILS_ROOT}/gpxfiles/#{file}")
      puts "#{RAILS_ROOT}/gpxfiles/#{file}"
      gpxfile = File.new("#{RAILS_ROOT}/gpxfiles/#{file}")
     
    end
   
    doc = XML::Document.file("#{RAILS_ROOT}/gpxfiles/#{file}")
    root = doc.root
    puts "Root element name: #{root.name}"
    puts root

    puts "im stuck outside"
    doc.find('//gpx/trk/trkseg/trkpt').each do |node|
      # puts "Node path: #{node.path} \t Contents: #{node.content}"
      puts "Im inside at least"
    end
    puts "im back outside"

# NOTE: only the outside puts are printed. no error messages either...

    # from our regxml days... this parses fine
    # doc.elements.each("gpx/trk/trkseg/trkpt") { |element|
    #   g = Geocode.new
    #   g.longitude = element.attributes["lon"]
    #   g.latitude   = element.attributes["lat"]
    #   g.save 
    #   geocodes << g
    # }

  end
end
------------
gpx snippet (cutting and pasting might screw the with the format - sorry if it's hard to read).
------------
Code:
<gpx xmlns="[URL unfurl="true"]http://www.topografix.com/GPX/1/0[/URL] " xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001[/URL]
/XMLSchema-instance" xmlns:mbx="[URL unfurl="true"]http://www.motionbased.net/mbx"[/URL] xsi:schemaLocati
on="[URL unfurl="true"]http://www.topografix.com/GPX/1/0[/URL] [URL unfurl="true"]http://www.topografix.com/GPX/1/0/gpx.xsd[/URL]
[URL unfurl="true"]http://www.motionbased.net/mbx[/URL] [URL unfurl="true"]http://www.motionbased.net/site/schemas/mbx/0.0.1[/URL]
/mbx.xsd" creator="MotionBased Technolgies [URL unfurl="true"]http://www.motionbased.com[/URL] " version="
1.0">
  <trk>
    <mbx:gpsTrackPk>610018</mbx:gpsTrackPk>
    <trkseg>
      <trkpt lat="37.71365" lon="-122.099394">
        <ele>90.0</ele>
        <time>2006-02-12T10:02:49-08:00</time>
        <course>180.00000500895632</course>
        <speed>0.38016656041145325</speed>
      </trkpt>
    </trkseg>
  </trk>
</gpx>

--------
AND ...
--------

The above will parse if I delete the xmlns= section to produce the following gpx tag
Code:
<gpx xsi:schemaLocati
on="[URL unfurl="true"]http://www.topografix.com/GPX/1/0[/URL] [URL unfurl="true"]http://www.topografix.com/GPX/1/0/gpx.xsd[/URL]
[URL unfurl="true"]http://www.motionbased.net/mbx[/URL] [URL unfurl="true"]http://www.motionbased.net/site/schemas/mbx/0.0.1[/URL]
/mbx.xsd" creator="MotionBased Technolgies [URL unfurl="true"]http://www.motionbased.com[/URL] " version="
1.0">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top