1
我一直在嘗試使用Nokogiri或ReXML來解析GPX文件,但看起來如此簡單的東西無法正常工作。我經常得到空數組或nil
或「未定義」輸出,當我確定它應該沒問題。我正在拉我的頭髮。有人能告訴我他們會怎麼做嗎?我想將lat
,lon
和ele
值的點提取到一個很好的乾淨哈希數組中。在Rails中存儲GPX文件?
這裏是(它有更多的積分)的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 Google地圖中顯示路線。但是,我也想將它作爲序列化的散列保存在我的數據庫中,這樣我可以查詢單個點。
這裏是我的引入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")
=> []
我在做什麼錯?