First, use a loop like this to walk through your XML:
doc.search('SHOPITEM').each do |shop_item|
item_id = shop_item.at('ITEM_ID').text
product = shop_item.at('PRODUCT').text
description = shop_item.at('DESCRIPTION').text
# ...
end
Each iteration that finds a <SHOPITEM>
tag will gather the information to create/modify a row.
Instead of using css
, which is equivalent to search
, you should use at
or at_css
. Both css
and search
return a NodeSet, which is like an array. If you use inner_text
on a NodeSet that contains more than one node, you'll get all the text nodes concatenated together as a string, which is most likely not what you want and will usually be a bug you have to fix:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
<foo>
<bar>1</bar>
<bar>2</bar>
</foo>
EOT
doc.search('bar').inner_text # => "12"
doc.css('bar').inner_text # => "12"
For more information, read the documentation for css
, search
, at
and at_css
in the Nokogiri::XML::Node page, along with the inner_text
documentation in the Nokogiri::XML::NodeSet page.