class Badgerfish::OxSaxParser
Public Instance Methods
attr(name, value)
click to toggle source
# File lib/badgerfish/ox_sax_parser.rb, line 55 def attr(name, value) if name.to_s.start_with?('xmlns') unless @remove_namespaces @root['@xmlns'] ||= {} if name.to_s.start_with? 'xmlns:' @root['@xmlns'][name[6, name.length]] = value else @root['@xmlns']['$'] = value end end else @root["@#{name}"] = value end end
end_element(name)
click to toggle source
# File lib/badgerfish/ox_sax_parser.rb, line 48 def end_element(name) (@root = @parents.pop).inject(@root) do |hash, (key, value)| hash[key] = nil if value.nil? || value.empty? hash end end
load(xml, options = {})
click to toggle source
# File lib/badgerfish/ox_sax_parser.rb, line 8 def load(xml, options = {}) @decode_html_entities = if options[:decode_html_entities].nil? true else !!options[:decode_html_entities] end @html_entities_coder = HTMLEntities.new @remove_namespaces = !!options[:remove_namespaces] @result = @root = {} @parents = [] Ox.sax_parse(self, StringIO.new(xml)) @result end
start_element(name)
click to toggle source
sax callbacks
# File lib/badgerfish/ox_sax_parser.rb, line 27 def start_element(name) new_element = {} name = name.to_s # force string representation of symbols if @remove_namespaces if colon_index = name.index(":") name.slice!(0..colon_index) end end if @root[name].nil? @root[name] = new_element else @root[name] = [@root[name]] unless @root[name].is_a?(Array) @root[name] << new_element end @parents << @root @root = new_element end
text(value)
click to toggle source
# File lib/badgerfish/ox_sax_parser.rb, line 71 def text(value) @root['$'] = @decode_html_entities ? @html_entities_coder.decode(value) : value end