require 'rexml/document'
doc = REXML::Document.new(File.new('input.xml'))
print doc
require 'rexml/document'
xml = <<EOL
<tree>
<trunk>
<leaf id="1">Leaf A</leaf>
<leaf id="2">Leaf B</leaf>
<leaf id="3">Leaf C</leaf>
</trunk>
</tree>
EOL
doc = REXML::Document.new xml
print doc
require 'rexml/document'
xml = <<EOL
<tree>
<trunk>
<leaf id="1">Leaf A</leaf>
<leaf id="2">Leaf B</leaf>
<leaf id="3">Leaf C</leaf>
</trunk>
</tree>
EOL
doc = REXML::Document.new(xml)
doc.elements.each('tree/trunk/leaf') { |element|
puts element
puts element.text
puts element.attributes['id']
}
<leaf id='1'>Leaf A</leaf>
Leaf A
1
<leaf id='2'>Leaf B</leaf>
Leaf B
2
<leaf id='3'>Leaf C</leaf>
Leaf C
3
指定する要素の階層が決まっていない場合は、REXML::XPath.each()
を使えば、位置に関係なくすべての要素を処理できます。
doc = REXML::Document.new(xml)
REXML::XPath.each(doc, '//leaf') { |element|
puts element
puts element.text
puts element.attributes['id']
}