Over a million developers have joined DZone.

Parse XML With Hpricot

·
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:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}