使用REXML读取XML文件

Ruby内置标准库中的REXML可以很方便的读取XML文件;今天查了一下相关资料,将其中主要部分登记下来备忘;

关键元素:
[color=blue]
element
elements[el_name]
attributes[attr_name]
[/color]

读取示例代码

require "rexml/document"

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

doc.elements.each("customers/customer") do |element|
puts element.elements["name"].text
puts element.attributes["title"]
end

你可能感兴趣的:(Ruby)