From: dima Date: 2007-08-31T06:40:04+09:00 Subject: Re: REXML Element exists question You should consider using XPaths capabilities of REXML. http://stdlib.rubyonrails.org/libdoc/rexml/rdoc/index.html http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/XPath.html http://www.ibm.com/developerworks/xml/library/x-matters18.html http://www.germane-software.com/software/rexml/docs/tutorial.html On Aug 30, 1:26 pm, Urban Hafner wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Aug 30, 2007, at 12:35 , John Butler wrote: > > > My question is there a more elegant way to this? I dont want to > > have to > > write an if/case statment for every possible element. I did try the > > below but that gave me an error on the nil? because it couldnt find > > the > > element to test if it was nil, thats why im suprised the above code > > works. > > > !element.elements["Client"].nil ? project.client = > > element.elements["Client"].text : project.client = nil > > I have no idea of REXML but you are missing a ? here. The one present is > the one of the ?: operator, but you need the .nil? method and not the > nil method so you have to add a second one. > > And if everything works like the above (i.e. the XML Tag has the same > name as the model attribute) then you could use something like this > (untested of course) and maybe stick it into a method: > > e = "Client" > meth = "#{e.downcase}=".to_sym > text = !element.elements[e].nil? ? element.elements[e].text : nil > project.send(meth, text) > > Urban > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.6 (Darwin) > > iD8DBQFG1qlsggNuVCIrEyURAp38AJ4y76fXMtYgKRA60ovmpdz0kuXt6QCfbtqr > 2SBTn6C4TlP/iCdQt14WJXI= > =YWwN > -----END PGP SIGNATURE-----