Array of Hashes (created by xmlsimple)

Discussion in 'Ruby' started by Maui Guide, Apr 17, 2009.

  1. Maui Guide

    Maui Guide Guest

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

    require 'xmlsimple'

    myxml = "<a href=\"http://www.mysite.com/category/file.php\">\r\n<img
    src=\"http://www.mysite.com/images/picture.jpg\" width=\"640\"
    height=\"480\" alt=\"\" border=\"0\"/></a>"

    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",
    "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!
    --
    Posted via http://www.ruby-forum.com/.
     
    Maui Guide, Apr 17, 2009
    #1
    1. Advertising

  2. 2009/4/17 Maui Guide <>:
    > I'm relatively new and this had me thoroughly confused today.
    >
    > require 'xmlsimple'
    >
    > myxml =3D "<a href=3D\"http://www.mysite.com/category/file.php\">\r\n<img
    > src=3D\"http://www.mysite.com/images/picture.jpg\" width=3D\"640\"
    > height=3D\"480\" alt=3D\"\" border=3D\"0\"/></a>"
    >
    > xmltags =3D XmlSimple.xml_in(myxml)
    >
    > href_tag =3D xmltags['href'] =A0 =A0 =A0 =A0 =A0 =A0 #<-- this works fine
    > img_tag =A0=3D xmltags['img'] =A0 =A0 =A0 =A0 =A0 =A0 =A0#<-- 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 =A0=3D xmltags['img']['src'] =A0 =A0 =A0 #<-- 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"=3D>"http://www.mysite.com/images/picture.jpg",
    > "border"=3D>"0", "height"=3D>"480", "alt"=3D>"", "width"=3D>"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 =3D xmltags['href'].first
    img_tag =3D xmltags['img'].first
    img_src =3D img_tag['src'] #<-- this should work

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 17, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hborda
    Replies:
    1
    Views:
    89
    David Vallner
    Nov 5, 2006
  2. slothbear
    Replies:
    2
    Views:
    126
    slothbear
    Jul 20, 2007
  3. kazaam
    Replies:
    12
    Views:
    281
    Matthias Wächter
    Sep 13, 2007
  4. Matt Brooks
    Replies:
    16
    Views:
    236
    Matt Brooks
    Sep 16, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    221
Loading...

Share This Page