xmlファイルを読む

test.xml

<test>
	<head version="1.2.0" size="512">
	</head>
	<foo index="0">
		foobar0
	</foo>
	<foo index="1" h="1">
		foobar1
	</foo>
</test>
require 'rexml/document'

file = File.new( "test.xml" )
doc = REXML::Document.new file

p doc.elements["test/head"].attributes["version"]
#=> "1.2.0"

p doc.elements["test/foo"].attributes["index"]
#=> "0"

doc.each_element("test/foo") {|e|
  p e.attributes["h"]
}
#=> nil
#=> "1"


=begin
# ↑と等価
doc.elements.each("test/foo") {|e|
  p e.attributes["h"]
}
=end

# 条件に一致するエレメントを取得する
p doc.elements["test/foo[@index='1']"].text
#=> "\n\t\tfoobar1\n\t"

取得したエレメントの親や兄弟を取得するメソッドもある.