繁体   English   中英

REXML红宝石的下一个元素

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM