soulcutter / saxerator

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

HashElement vs StringElement #12

Closed voke closed 10 years ago

voke commented 10 years ago

Suppose you read a XML file with some blank nodes:

require 'saxerator'

data = <<-DATA
  <products>
    <product>
      <name>iPhone 5S</name>
      <ean>1234567890</ean>
    </product>
    <product>
      <name>XBOX 360</name>
      <ean />
    </product>
  </products>
DATA

parser = Saxerator.parser(data)

parser.for_tag(:product).each do |item|
  p item['ean']
end

# => "1234567890"
# => {}

Wouldn't it make more sense to return a StringElement with the value of nil instead of returning an empty HashElement? It won't work well with the put_attributes_in_hash! option but anyways.

soulcutter commented 10 years ago

I think I agree - that should probably be a StringElement

soulcutter commented 10 years ago

I finally got back to thinking about this, and I think #13 (EmptyElement) should provide the ability to treat parsing results more-consistently. Let me know if you see any trouble with that solution.

Thanks for your feedback!