From: Childs Childs Date: 2010-09-25T10:07:55+09:00 Subject: Re: Processing XML Mi Sp wrote: > unknown wrote: >> On Thu, Sep 23, 2010 at 8:50 PM, Mi Sp wrote: >>> >>> >>> >>> prod1 >>> prodid1 >>> >>> >>> I am trying to use REXML via >>> doc.elements.each("*/doc") { |element| puts element.attributes["prod1"] >>> } >> >> "doc" doesn't have an attribute of prod1 (no one does actually). For >> example: >> >>>> REXML::Document.new(x).elements.each("*/doc"){|e| e.elements.each("field"){|f| p f.attributes; puts f.text}} >> {"name"=>name='id'} >> prod1 >> {"name"=>name='prodId'} >> prodid1 >> {"name"=>name='id'} >> prod2 >> {"name"=>name='prodId'} >> prodid2 > > Thanks for the quick response. You are correct - I meant to say: > doc.elements.each("*/doc") { |element| puts > element.attributes["prodId"]} > > Any ideas on how I retrieve the field value for this attribute? Heh...I just registered so I could post on the roughly the same subject, but this seems to work: #!/usr/bin/ruby -w require 'rexml/document' include REXML file = File.open("forum.xml") doc = Document.new file root = doc.root root.elements.each("doc/field[@name='prodId']") do |element| puts element.text end -- Posted via http://www.ruby-forum.com/.