Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Parse XML With Hpricot

DZone's Guide to

Parse XML With Hpricot

·
Free Resource
From http://errtheblog.com/post/8
Simple XML is basically HTML with random tags, yeah?  Parse it with Hpricot!

Your XML:


  
  
   
    
    
     403276
    
    
    
     Trivet
    
    
    
     0
    
    
    
     0
    
  
   

  


The code:

require 'hpricot'
FIELDS = %w[SKU ItemName CollectionNo Pages]

doc = Hpricot.parse(File.read("my.xml"))
(doc/:product).each do |xml_product|
  product = Product.new
  for field in FIELDS
    product[field] = (xml_product/field.intern).first.innerHTML
  end
  product.save
end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}