require 'rexml/node' require 'rexml/element' require 'rexml/document' require 'rexml/encoding' include REXML #Open an existing XML file. file = File.new("example1.xml","r") doc = REXML::Document.new(file) cardatabase = doc.root #add an attribute to each car cardatabase.each_element { |car| car.add_attribute( "owner", "Dennis") } #show the owner of each car puts "Dennis owns: " cardatabase.each_element { |car| if(car.attributes['owner'] == "Dennis") puts car.attributes['make'] end } #find the make of the 'Yellow' car puts puts "The yellow car is the " + cardatabase.elements["car[@color='Yellow']"].attributes['make'] #change the 'owner' attribute puts puts "Select a car: " make = gets.chomp if(cardatabase.elements["car[@make='#{make}']"] != nil) puts "Give the #{make} to: " name = gets.chomp cardatabase.elements["car[@make='#{make}']"].attributes['owner'] = name puts "The owner of the #{make} is now " + cardatabase.elements["car[@make='#{make}']"].attributes['owner'] else puts "The #{make} was not found." end file2 = File.new("example2.xml","w") file2.write(doc)