Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

ruby xml => array using hpricot (See related posts)

// description of your code here
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}      

Comments on this post

charlmatthee posts on Apr 05, 2009 at 00:10
Use the name method on an element object to get the tag name. So, based on what you have above:

tag = e.to_s.split('<')[1].split('>')[0]


becomes:

tag = e.name

You need to create an account or log in to post comments to this site.


Related Posts