this code takes a chunk of xml representing a recordset pulled from a database by a website
and convert it into a ruby array
require 'rubygems' require 'hpricot' xml = %{ <query> <status> <id>1</id> <created_at>a date</created_at> <text>some text</text> </status> <status> <id>2</id> <created_at>a date 2</created_at> <text>some text 1</text> </status> <status> <id>3</id> <created_at>a date 3</created_at> <text>some text 2</text> </status> </query> } record = Array.new recordset = Array.new doc = Hpricot::XML(xml) # add a row with column names # for beginners: notice that :query and :status match the xml structure names = (doc/:query/:status)[0] # names is an Array that contains sub part of the XML (between <status></status>) names.each_child do |e| if e.innerHTML != nil then # => because carriage return between <tag></tag> are considered elements too # strange hpricot deos not provide a method to get the tag name # or i cant read the doc ?? tag = e.to_s.split('<')[1].split('>')[0] record << tag end end recordset << record record = [] # add rows with data (doc/:query/:status).each do |status| status.each_child do |e| if e.innerHTML != nil then record << e.innerHTML end end recordset << record record = [] end recordset.each { |r| p r}
tag = e.to_s.split('<')[1].split('>')[0]
becomes:
tag = e.name