1

我一直在尝试使用 Nokogiri 或 ReXML 解析 GPX 文件,但看起来如此简单的方法却行不通。nil当我确定它应该没问题时,我经常得到空数组或“未定义”输出。我正在拔头发。有人可以告诉我他们会怎么做吗?我想将带有lat,lonele值的点提取到一个干净的哈希数组中。

这是 XML(它有更多的点):

<?xml version="1.0"?>
<gpx creator="GPS Visualizer http://www.gpsvisualizer.com/" version="1.0" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<trk>
  <name>20090426-144136</name>
  <trkseg>
    <trkpt lat="54.0663350" lon="-2.0050000">
      <ele>171.1</ele>
    </trkpt>
    <trkpt lat="54.0663400" lon="-2.0050000">
      <ele>172.8</ele>
    </trkpt>
    <trkpt lat="54.0663380" lon="-2.0049970">
      <ele>172</ele>
    </trkpt>
  </trkseg>
</trk>
</gpx>

这就是我想要得到的:

:route => [{ :lat => 54.0663350, :lon => -2.0050000, :ele => 171.1 },
 { :lat => 54.0663400, :lon => -2.0050000, :ele => 172.8 }, ...]

或类似的(如果您有更好的建议)。

最终,数据将以 JSON 格式发送,以在 JavaScript 谷歌地图中显示路线。但是,我还想将它作为序列化哈希保存在我的数据库中,这样我就可以查询单点。

这是我与 Nokogiri 的令人头疼的时刻之一:

1.9.3p194 :179 > doc = Nokogiri::XML(File.open("gpx/short.gpx"))
 => #<Nokogiri::XML::Document:0x816dbe34 name="document" children=[#<Nokogiri::XML::Element:0x816dba38 name="gpx" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816db948 name="creator" value="GPS Visualizer http://www.gpsvisualizer.com/">, #<Nokogiri::XML::Attr:0x816db934 name="version" value="1.0">, #<Nokogiri::XML::Attr:0x816db920 name="schemaLocation" namespace=#<Nokogiri::XML::Namespace:0x816dad40 prefix="xsi" href="http://www.w3.org/2001/XMLSchema-instance"> value="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">] children=[#<Nokogiri::XML::Text:0x816da3f4 "\n">, #<Nokogiri::XML::Element:0x816da278 name="trk" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d9da0 "\n  ">, #<Nokogiri::XML::Element:0x816d9c60 name="name" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d95a8 "20090426-144136">]>, #<Nokogiri::XML::Text:0x816d9148 "\n  ">, #<Nokogiri::XML::Element:0x816d901c name="trkseg" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d8a7c "\n    ">, #<Nokogiri::XML::Element:0x816d898c name="trkpt" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816d8928 name="lat" value="54.0663350">, #<Nokogiri::XML::Attr:0x816d8914 name="lon" value="-2.0050000">] children=[#<Nokogiri::XML::Text:0x816d7a3c "\n      ">, #<Nokogiri::XML::Element:0x816d7938 name="ele" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d7370 "171.1">]>, #<Nokogiri::XML::Text:0x816d712c "\n    ">]>, #<Nokogiri::XML::Text:0x816d6e0c "\n    ">, #<Nokogiri::XML::Element:0x816d6ccc name="trkpt" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816d6c54 name="lat" value="54.0663400">, #<Nokogiri::XML::Attr:0x816d6bf0 name="lon" value="-2.0050000">] children=[#<Nokogiri::XML::Text:0x816d5db8 "\n      ">, #<Nokogiri::XML::Element:0x816d5cb4 name="ele" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d57c8 "172.8">]>, #<Nokogiri::XML::Text:0x816d5430 "\n    ">]>, #<Nokogiri::XML::Text:0x816d500c "\n    ">, #<Nokogiri::XML::Element:0x816d4eb8 name="trkpt" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816d4e54 name="lat" value="54.0663380">, #<Nokogiri::XML::Attr:0x816d4e40 name="lon" value="-2.0049970">] children=[#<Nokogiri::XML::Text:0x816d41e8 "\n      ">, #<Nokogiri::XML::Element:0x816d40a8 name="ele" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816bafe0 "172">]>, #<Nokogiri::XML::Text:0x816bacd4 "\n    ">]>, #<Nokogiri::XML::Text:0x816ba9c8 "\n  ">]>, #<Nokogiri::XML::Text:0x816ba734 "\n">]>, #<Nokogiri::XML::Text:0x816ba4c8 "\n">]>]> 
1.9.3p194 :205 > doc.xpath("*")
 => [#<Nokogiri::XML::Element:0x816dba38 name="gpx" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816db948 name="creator" value="GPS Visualizer http://www.gpsvisualizer.com/">, #<Nokogiri::XML::Attr:0x816db934 name="version" value="1.0">, #<Nokogiri::XML::Attr:0x816db920 name="schemaLocation" namespace=#<Nokogiri::XML::Namespace:0x816dad40 prefix="xsi" href="http://www.w3.org/2001/XMLSchema-instance"> value="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">] children=[#<Nokogiri::XML::Text:0x816da3f4 "\n">, #<Nokogiri::XML::Element:0x816da278 name="trk" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d9da0 "\n  ">, #<Nokogiri::XML::Element:0x816d9c60 name="name" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d95a8 "20090426-144136">]>, #<Nokogiri::XML::Text:0x816d9148 "\n  ">, #<Nokogiri::XML::Element:0x816d901c name="trkseg" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d8a7c "\n    ">, #<Nokogiri::XML::Element:0x816d898c name="trkpt" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816d8928 name="lat" value="54.0663350">, #<Nokogiri::XML::Attr:0x816d8914 name="lon" value="-2.0050000">] children=[#<Nokogiri::XML::Text:0x816d7a3c "\n      ">, #<Nokogiri::XML::Element:0x816d7938 name="ele" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d7370 "171.1">]>, #<Nokogiri::XML::Text:0x816d712c "\n    ">]>, #<Nokogiri::XML::Text:0x816d6e0c "\n    ">, #<Nokogiri::XML::Element:0x816d6ccc name="trkpt" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816d6c54 name="lat" value="54.0663400">, #<Nokogiri::XML::Attr:0x816d6bf0 name="lon" value="-2.0050000">] children=[#<Nokogiri::XML::Text:0x816d5db8 "\n      ">, #<Nokogiri::XML::Element:0x816d5cb4 name="ele" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816d57c8 "172.8">]>, #<Nokogiri::XML::Text:0x816d5430 "\n    ">]>, #<Nokogiri::XML::Text:0x816d500c "\n    ">, #<Nokogiri::XML::Element:0x816d4eb8 name="trkpt" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> attributes=[#<Nokogiri::XML::Attr:0x816d4e54 name="lat" value="54.0663380">, #<Nokogiri::XML::Attr:0x816d4e40 name="lon" value="-2.0049970">] children=[#<Nokogiri::XML::Text:0x816d41e8 "\n      ">, #<Nokogiri::XML::Element:0x816d40a8 name="ele" namespace=#<Nokogiri::XML::Namespace:0x816db970 href="http://www.topografix.com/GPX/1/0"> children=[#<Nokogiri::XML::Text:0x816bafe0 "172">]>, #<Nokogiri::XML::Text:0x816bacd4 "\n    ">]>, #<Nokogiri::XML::Text:0x816ba9c8 "\n  ">]>, #<Nokogiri::XML::Text:0x816ba734 "\n">]>, #<Nokogiri::XML::Text:0x816ba4c8 "\n">]>] 
1.9.3p194 :206 > doc.xpath("*/trk")
 => [] 
1.9.3p194 :207 > doc.xpath("//trk")
 => [] 
1.9.3p194 :208 > doc.xpath("trk")
 => [] 
1.9.3p194 :209 > doc.xpath("//trkpt")
 => []
1.9.3p194 :210 > doc.xpath("*/gpx")
 => [] 
1.9.3p194 :211 > doc.xpath("*/gpx/trk")
 => [] 

我究竟做错了什么?

4

1 回答 1

1

在“ Ruby GPX 文件解析器”的帮助下,我发现了问题:我必须为我的 XML 标签声明命名空间:doc.xpath('//xmlns:trkpt')而不是doc.xpath('//trkpt').

这是我在 Rails 模型中的方法:

def self.parse_gpx(filename)
  file = File.open(filename)
  doc = Nokogiri::XML(file)
  trackpoints = doc.xpath('//xmlns:trkpt')
  route = Array.new
  trackpoints.each do |trkpt|
    lat = trkpt.xpath('@lat').to_s.to_f
    lon = trkpt.xpath('@lon').to_s.to_f
    ele = trkpt.text.strip.to_f
    route << {lat: lat, lon: lon, ele: ele}
  end
  route
end

这是我的输出:

1.9.3p194 :329 > Route::parse_gpx("gpx/short.gpx")
 => [{:lat=>54.066335, :lon=>-2.005, :ele=>171.1}, {:lat=>54.06634, :lon=>-2.005, :ele=>172.8}, {:lat=>54.066338, :lon=>-2.004997, :ele=>172.0}]
于 2012-11-23T23:56:37.237 回答