RosettaCodeData/Task/XML-Input/Ruby/xml-input.rb

15 lines
317 B
Ruby
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
require 'rexml/document'
include REXML
doc = Document.new(File.new("sample.xml"))
# or
# doc = Document.new(xml_string)
# without using xpath
doc.each_recursive do |node|
puts node.attributes["Name"] if node.name == "Student"
end
# using xpath
doc.each_element("*/Student") {|node| puts node.attributes["Name"]}