views:

15

answers:

1

I have an xml element with multiple child elements that use the same ID. The child elements are distinguishable by a "size" attribute. When I create a hash using .from_xml it lumps all these child elements 1 field. How do I parse these child elements into different fields based on the xml attribute?

XML:

<artist rank="48">
 <name>KISS</name>
  <image size="small">http://userserve-ak.last.fm/serve/34/3329738.jpg&lt;/image&gt;
  <image size="medium">http://userserve-ak.last.fm/serve/64/3329738.jpg&lt;/image&gt;
  <image size="large">http://userserve-ak.last.fm/serve/126/3329738.jpg&lt;/image&gt;
  <image size="extralarge">http://userserve-ak.last.fm/serve/252/3329738.jpg&lt;/image&gt;
  <image size="mega">http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg&lt;/image&gt;
</artist>

Ruby:

>>xml = "<artist rank='48'><name>KISS</name><image size='small'>http://userserve-ak.last.fm/serve/34/3329738.jpg&lt;/image&gt;&lt;image size='medium'>http://userserve-ak.last.fm/serve/64/3329738.jpg&lt;/image&gt;&lt;image size='large'>http://userserve-ak.last.fm/serve/126/3329738.jpg&lt;/image&gt;&lt;image size='extralarge'>http://userserve-ak.last.fm/serve/252/3329738.jpg&lt;/image&gt;&lt;image size='mega'>http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg&lt;/image&gt;&lt;/artist&gt;"
=> "<artist rank='48'><name>KISS</name><image size='small'>http://userserve-ak.last.fm/serve/34/3329738.jpg&lt;/image&gt;&lt;image size='medium'>http://userserve-ak.last.fm/serve/64/3329738.jpg&lt;/image&gt;&lt;image size='large'>http://userserve-ak.last.fm/serve/126/3329738.jpg&lt;/image&gt;&lt;image size='extralarge'>http://userserve-ak.last.fm/serve/252/3329738.jpg&lt;/image&gt;&lt;image size='mega'>http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg&lt;/image&gt;&lt;/artist&gt;"
>>h = Hash.from_xml xml
=> {"artist"=>{"name"=>"KISS", "rank"=>"48", "image"=>["http://userserve-ak.last.fm/serve/34/3329738.jpg", "http://userserve-ak.last.fm/serve/64/3329738.jpg", "http://userserve-ak.last.fm/serve/126/3329738.jpg", "http://userserve-ak.last.fm/serve/252/3329738.jpg", "http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg"]}}

What I want:

=> {"artist"=>{"name"=>"KISS", "rank"=>"48", "image_small"=>"http://userserve-ak.last.fm/serve/34/3329738.jpg", "image_medium"="http://userserve-ak.last.fm/serve/64/3329738.jpg", "image_large"="http://userserve-ak.last.fm/serve/126/3329738.jpg", "image_extralarge"="http://userserve-ak.last.fm/serve/252/3329738.jpg", image_mega"="http://userserve-ak.last.fm/serve/500/3329738/KISS+04.jpg"}}
A: 

Hash.to_xml currently won't do that. It's really just meant for the simplest cases.

But, there's an open ticket with some proposed patches. Still, I would recommend something like Nokogiri to parse your XML.

Andrew Vit
Thanks, I will look into this. FWIW, from_xml added the image elements as an array. So I was able to access the content for each image element using the array location, example: h['artist']['image'][1]
Slomojamma