Sha256: 20f88c553e844ae5fe5c103dfdba43926c74953fd3f3850396e016a038e78067
Contents?: true
Size: 1.17 KB
Versions: 1
Compression:
Stored size: 1.17 KB
Contents
require 'ox' require 'stringio' module Badgerfish class OxSaxParser < Ox::Sax def load(xml) @result = @root = {} @parents = [] Ox.sax_parse(self, StringIO.new(xml)) @result end # # sax callbacks # def start_element(name) new_element = {} name = name.to_s # force string representation of symbols if @root[name].nil? @root[name] = new_element else @root[name] = [@root[name]] unless @root[name].is_a?(Array) @root[name].push new_element end @parents.push @root @root = new_element end def end_element(name) (@root = @parents.pop).inject(@root) do |memo, (key, value)| memo.tap do |hash| hash[key] = nil if value.empty? end end end def attr(name, value) unless name.to_s.start_with? 'xmlns' @root["@#{name}"] = value else @root['@xmlns'] ||= {} if name.to_s.start_with? 'xmlns:' @root['@xmlns'][name[6, name.length]] = value else @root['@xmlns']['$'] = value end end end def text(value) @root['$'] = value end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
badgerfish-0.1.0 | lib/badgerfish/ox_sax_parser.rb |