# Array of Hashes (created by xmlsimple)

**URL:** <https://www.ruby-forum.com/t/array-of-hashes-created-by-xmlsimple/165779>\
**Category:** Ruby\
**Created:** [April 17, 2009, 11:47am UTC](https://www.ruby-forum.com/t/array-of-hashes-created-by-xmlsimple/165779 "2009-04-17T11:47:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![newtoruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/50afbb/32.png) [@newtoruby](https://www.ruby-forum.com/u/newtoruby)\
**Post date:** [April 17, 2009, 11:47am UTC](https://www.ruby-forum.com/t/array-of-hashes-created-by-xmlsimple/165779/1 "2009-04-17T11:47:27Z")

</div>

I’m relatively new and this had me thoroughly confused today.

require ‘xmlsimple’

myxml = “\<a href="[http://www.mysite.com/category/file.php\](http://www.mysite.com/category/file.php%5C)”\>\r\n\<img  
src="[http://www.mysite.com/images/picture.jpg\](http://www.mysite.com/images/picture.jpg%5C)" width="640"  
height="480" alt="" border="0"/\>"

xmltags = XmlSimple.xml\_in(myxml)

href\_tag = xmltags[‘href’] #\<-- this works fine  
img\_tag = xmltags[‘img’] #\<-- this works fine  
img\_src = xmltags[‘img’][‘src’] #\<-- this doesn’t work

irb(main):221:0\> p img\_tag  
[{“src”=\>“[http://www.mysite.com/images/picture.jpg](http://www.mysite.com/images/picture.jpg)”,  
“border”=\>“0”, “height”=\>“480”, “alt”=\>“”, “width”=\>“640”}]

I can loop through all the hashes in the array (via img\_tag.each do |t|)  
But isn’t there a way to access the img src attribute directly?

Thanks!

---

<div class="post-metadata">

**Author:** ![newtoruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/50afbb/32.png) [@newtoruby](https://www.ruby-forum.com/u/newtoruby)\
**Post date:** [April 17, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/array-of-hashes-created-by-xmlsimple/165779/2 "2009-04-17T15:26:24Z")

</div>

2009/4/17 Maui G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> href\_tag = xmltags[‘href’] #\<-- this works fine  
> img\_tag = xmltags[‘img’] #\<-- this works fine

Actually I believe the namings of your variables to be misleading:  
there should be an “s” at the end because what you get are multiple  
elements. Which is also the explanation why

> img\_src = xmltags[‘img’][‘src’] #\<-- this doesn’t work

Also, it seems there is no point in traversing the XML document again  
to get the same “img” tags again.

> irb(main):221:0\> p img\_tag  
> [{“src”=\>“[http://www.mysite.com/images/picture.jpg](http://www.mysite.com/images/picture.jpg)”,  
> “border”=\>“0”, “height”=\>“480”, “alt”=\>“”, “width”=\>“640”}]
> 
> I can loop through all the hashes in the array (via img\_tag.each do |t|)  
> But isn’t there a way to access the img src attribute directly?

You need to decide whether you expect one or many tags. If you are  
interested in one only and if it is guaranteed that there is exactly  
one present you can do

href\_tag = xmltags[‘href’].first  
img\_tag = xmltags[‘img’].first  
img\_src = img\_tag[‘src’] #\<-- this should work

Cheers

robert
