From: Phrogz Date: 2008-01-24T08:19:57+09:00 Subject: Re: Parsing poorly written XML On Jan 23, 4:07 pm, Phrogz wrote: > On Jan 23, 3:35 pm, Patrick Callahan wrote: > > > I've found an application which dumps all the registry settings for > > installed applications into an .xml file for me (you can find it as > > "myuninstall" via google). > > > I can't figure out the ruby code I need from REXML to do the following: > > -find a particular "product_name" in the xml file > > -if found, save off the "uninstall_string" so I can execute it on the > > commandline and uninstall the app. > > require 'rexml/document' > doc = REXML::Document.new( DATA.read ) > > # Find 'item' elements at any level > # that have a child 'name' element whose text value is 'foo' > doc.each_element( "//item[name='foo']" ){ |item| > # Find every child element of the item element > # whose name is 'value', get the first one, and get its text > puts item.get_elements( "value" ).first.text} > > #=> 42 > #=> 17 > > __END__ > > > foo > 42 > > > bar > 17 > > > foo > 17 > > Or, using XPath more liberally (with the same DATA section as quoted above): require 'rexml/document' doc = REXML::Document.new( DATA.read ) p REXML::XPath.match( doc, "//item[name='foo']/value/text()" ) #=> ["42", "17"]