xml - Ruby - Finding Elements with REXML -
i have xml file holds information retail stores in particular city , locations i'm having trouble retrieving data it. problem can find stores names looping through name elements don't know how corresponding addresses once find stores i'm looking for. know how this? i'm using rexml main xml module. here's xml file , code like
xml:
<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>
ruby:
xml_file.elements.each('stores/store/name') |name| if input == name print name + "\n" #code retrieve address print address + "\n\n" end end
xml_file = document.new file.new("myxml.xml") xml_file.elements.each("stores/store") |element| if element.attributes["name"] == input puts element.attributes["address"] end end
attributes
<store store_name="mystore"
to
element.attributes["store_name"]
edit, sorry try this
xml_file.elements.each("stores/store") |element| if element.name.text == input puts element.address.text end end
based on comment, correct syntax dialog.elements['name'].text
Comments
Post a Comment