soulcutter / saxerator

A SAX-based XML parser for parsing large files into manageable chunks
MIT License
128 stars 19 forks source link

XML attribute not copied. #6

Closed jrz closed 10 years ago

jrz commented 10 years ago

When saxing using xml, the attributes of the current root element is not accessible, even though it prints using element.to_s:

parser.for_tag(:product).each do |offer|
puts offer.attributes('id').value
end

Doesn't work. but:

parser.for_tag(:product).each do |offer|
offer = Nokogiri::XML(offer.to_s).elements[0]
puts offer.attributes('id').value
end

does.

xml:

<products>
<product id="1"><name>test1</name><.product>
<product id="2"><name>test2</name><.product>
<product id="3"><name>test3</name><.product>
<products>
soulcutter commented 10 years ago

Very strange! I'll take a look at what is going on with this.

soulcutter commented 10 years ago

I took a look at this, and I think you're perhaps expecting to receive a Nokogiri::XML::Element but what actually comes back is a Nokogiri::XML::Document. You can access the attributes via:

parser.for_tag(:product).each do |offer|
  puts offer.elements.first.attributes['id'].value
end