Parse XML With Hpricot
Join the DZone community and get the full member experience.
Join For FreeFrom 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
XML
Opinions expressed by DZone contributors are their own.
Comments