[英]REXML ruby next element
我正在嘗試從XML獲取元素和下一個元素
<way>
<nd ref="4979923479"/>
<nd ref="4979923478"/>
<nd ref="5721236634"/>
<nd ref="5721236635"/>
<nd ref="5721236636"/>
<nd ref="5721236637"/>
<nd ref="4979923477"/>
<nd ref="5721236638"/>
<nd ref="5721236639"/>
</way>
在這里,我嘗試做的是,而不是“ puts i.attributes [“ ref”]”,我需要的是“ puts ## i.attributes [“ ref”]}-> i.next(i + 1) .attributes [ “REF”]
require "rexml/document"
include REXML
inputFileName = ARGV[0]
file = File.new(inputFileName)
doc = Document.new(file)
doc.elements.each("way/nd") do |i|
if i.next != nil
puts i.attributes["ref"]
end
end
實際輸出只是所有nd的列表
4979923479
4979923478
5721236634
5721236635
5721236636
5721236637
4979923477
5721236638
5721236639
所需的輸出是:
4979923479 -> 4979923478
4979923478 -> 5721236634
5721236634 -> 5721236635
5721236635 -> 5721236636
5721236636 -> 5721236637
5721236637 -> 4979923477
4979923477 -> 5721236638
5721236638 -> 5721236639
我認為您想使用next_element
代替。 這將產生所需的輸出:
require "rexml/document"
include REXML
inputFileName = ARGV[0]
file = File.new(inputFileName)
doc = Document.new(file)
doc.elements.each("way/nd") do |i|
next unless i.next_element
puts "#{i.attributes["ref"]} -> #{i.next_element.attributes['ref']}"
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.