SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Mar 7, 2009, 01:22 #1
- Join Date
- Aug 2003
- Location
- Southern California, United States
- Posts
- 1,616
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Mar 10, 2009, 08:21 #2
- Join Date
- Mar 2009
- Posts
- 10
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Use this
Code:h = {}
Code:h = []
You might also want to check out XML-Simple, which parses XML into easy-to-use hashes.
-
Apr 8, 2009, 01:34 #3
- 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