修改XML属性值

这个就没什么好讲的了,注意用法就行了。

 

include REXML
  def index
    doc = Document.new("<root><breakfast type='continental'/></root>")
    breakfast = doc.root.elements['breakfast']
    breakfast_type = breakfast.attribute('type')
    puts breakfast_type.value  # => continental
    breakfast_type.value = 'full english' #这是错误写法
    # => NoMethodError: undefined method `value=' for type='continental':REXML::Attribute
    breakfast.add_attribute('type', 'full english')  #正确写法
    puts breakfast  # => <breakfast type='full english'/>
  end

 

你可能感兴趣的:(xml)