Ruby - Finding Elements with REXML

Fred69

New Member
I have an XML file that holds information about retail stores in a particular city and their locations but I'm having some trouble retrieving data from it. The problem is I can find stores by their names by looping through the name elements but I don't know how to get the corresponding addresses once I find the stores I'm looking for. Does anyone know how to do this? I'm using REXML as my main XML module. Here's what my XML file and my code look likeXML:\[code\]<stores> <store> <name>Supermarket</name> <address>2136 56th Avenue</address> </store> <store> <name>Convenience Store</name> <address>503 East Avenue</address> </store> <store> <name>Pharmacy</name> <address>212 Main Street</address> </store></stores>\[/code\]Ruby:\[code\]xml_file.elements.each('stores/store/name') do |name| if input == name print name + "\n" #code to retrieve the address print address + "\n\n" endend\[/code\]
 
Back
Top