soulcutter/saxerator

HashElement vs StringElement

voke opened this issue · 2 comments

voke commented

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.

I think I agree - that should probably be a StringElement

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!