我正在尝试读取 XML 文件并将结构存储到对象数组中。这是我的代码:
class Bike
attr_accessor :id, :color, :name
def initialize(id, color, name)
@id = id
@color = color
@name = name
end
end
---x---snip---x---
rules.root.each_element do |node1|
case node1.name
when "Bike"
bike = Bike.new(node1.attributes['id'], node1.attributes['color'], { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name"})
bikes.push bike
end
end
但是,最后一个元素并不是单独获取值。它正在获取整个标签。有更好的方法吗?