SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help with forming a multidimension array from an XML parse.

    My XML document is as follows (per SeeqPod API):
    <?xml version='1.0' encoding='UTF-8' ?>
    <playlist version="1" xmlns="http://xspf.org/ns/0/" start="0" end="0" total="26" prev="None" next="1">
    <title></title>
    <trackList>
    <track>
    <location>http://joniverse.com/resources/music/u2-one.mp3</location>
    <title>One</title>
    <creator>U2</creator>
    <extension application="seeqpod_metadata">
    <mp3_url_id>19646033</mp3_url_id>
    <has_music_rex>True</has_music_rex>
    <song_id>39445</song_id>
    </extension>
    </track>
    </trackList>
    </playlist>

    I just cant seem to make an array of @songs from it and I have tried with this code but I get no success:
    Code Ruby:
    				xml = Net::HTTP.get(URI(url))
    				songs = REXML::Document.new xml
    				@songs = []
    				songs.elements.each('playlist/trackList/track') do |s|
    					h = []
    					%w[location title creator].each do |e|
    						h[e] = s.elements[e].text
    					end
    					@songs << h
    				end

    Does anyone know what I am doing wrong? I have been fussing with this for hours...
    Have a good day.

  2. #2
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this
    Code:
    h = {}
    instead of
    Code:
    h = []
    - you're creating a hash, not an array.

    You might also want to check out XML-Simple, which parses XML into easy-to-use hashes.

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also you may like to check out Hpricot or Nokogiri for XML parsing with plenty of grunt.

    These two are especially awesome if you understand XPath and CSS selectors.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •