From: Robert Klemme Date: 2009-03-18T21:10:25+09:00 Subject: Re: REXML get specific element 2009/3/18 Pierre Pat : > All, > > I'm trying for the first time to play with XML and Ruby. > Since I have only the core library at the moment, my choice went > directly for REXML. > > So, here is my problem. > I'm willing to get a specific element in my xml file. > > Normally I would do something like this: > doc.root.elements["programmer[@name='Matz']"] > > But let's say I have a user interface, and I let a user enter the > programmer he wants to see. How can I look for an element using a > variable? > I hoped something like this would work: > entered_name = 'Matz' > doc.root.elements["magician[@name=#{entered_name}]"] You forgot the single quotes around #{}, i.e. you must only replace "Matz" with "#{entered_name}": doc.root.elements["programmer[@name='#{entered_name}']"] > Unfortunately, it didn't... > > After trying a few things, check with uncle google and in this forum, > I'm driving out of clues, so I'd appreciate if someone could show me the > correct way! Cheers robert -- remember.guy do |as, often| as.you_can - without end