Forum: Ruby on Rails Rails 1.2

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9cf5ebfbbcb05ee700b02377206b651b?d=identicon&s=25 Luke I. (technodolt)
on 2007-01-10 22:23
Ok, I have no clue where to post this, so if I'm in the wrong forum,
just tell me, and I'll take a hike.

That being said:
I was looking at the rubyonrails.org weblog site, and was reading of the
updates to Active Support.

One that caught my attention was the Hash.from_xml function... as I was
talking it over with another programmer, I got to wondering, how would
it deal with something like:

<listofnames>
  <name>Bob</name>
  <name>Jim</name>
  <name>Frank</name>
</listofnames>

The structure they showed seemed to imply that a hash would be created
using the tag name as the key, and the information contained in the tag
as the value.  So if there are three tags named 'name', does you just
get {:listofnames => {:name => "Frank"}}?

I also tried to post this in Rails Engines, but apparently you have to
be a member of that list, which I don't want to be unless absolutely
necessary (not from any reason other than if I'm only going to post to
it once, I don't see the need to become a member).
James Stewart (Guest)
on 2007-01-10 22:29
(Received via mailing list)
On Jan 10, 2007, at 4:23 PM, Luke Ivers wrote:
> talking it over with another programmer, I got to wondering, how would
> tag
> as the value.  So if there are three tags named 'name', does you just
> get {:listofnames => {:name => "Frank"}}?

If there are multiple elements with the same name then rails will
return those as an array:

$ script/console

 >> xml = '<listofnames>
   <name>Bob</name>
   <name>Jim</name>
   <name>Frank</name>
</listofnames>'
=> "<listofnames>\n  <name>Bob</name>\n  <name>Jim</name>\n
<name>Frank</name>\n</listofnames>"
 >> a = Hash.from_xml(xml)
=> {"listofnames"=>{"name"=>["Bob", "Jim", "Frank"]}}

James.

--
James Stewart
blogging at http://jystewart.net
0efceceb58496305ce9537f5251da6fb?d=identicon&s=25 Dan M. (dcmanges)
on 2007-01-10 22:32
(Received via mailing list)
Luke Ivers wrote:
> One that caught my attention was the Hash.from_xml function... as I was
> talking it over with another programmer, I got to wondering, how would
> it deal with something like:
>
> <listofnames>
>   <name>Bob</name>
>   <name>Jim</name>
>   <name>Frank</name>
> </listofnames>

>> Hash.from_xml("<listofnames><name>Bob</name><name>Jim</name></listofnames>")
=> {"listofnames"=>{"name"=>["Bob", "Jim"]}}

It doesn't take long to just try something out. :-)  I recommend
setting up a project using "edge Rails" so you can easily do this.

Dan Manges
9cf5ebfbbcb05ee700b02377206b651b?d=identicon&s=25 Luke I. (technodolt)
on 2007-01-10 22:34
Dan Manges wrote:
> Luke Ivers wrote:
>> One that caught my attention was the Hash.from_xml function... as I was
>> talking it over with another programmer, I got to wondering, how would
>> it deal with something like:
>>
>> <listofnames>
>>   <name>Bob</name>
>>   <name>Jim</name>
>>   <name>Frank</name>
>> </listofnames>
>
>>> Hash.from_xml("<listofnames><name>Bob</name><name>Jim</name></listofnames>")
> => {"listofnames"=>{"name"=>["Bob", "Jim"]}}
>
> It doesn't take long to just try something out. :-)  I recommend
> setting up a project using "edge Rails" so you can easily do this.
>
> Dan Manges

Thanks to you, and also to James.
Still new enough to the whole thing that doing that didn't occur to me;
I'll do so in the future, though [I try to only make mistakes that make
me appear a completely clueless newbie once].
This topic is locked and can not be replied to.