Sha256: 7f6a0da6ed590db315621151c3f4166735e87bed95b8a19ac339b8a829cbab98

Contents?: true

Size: 999 Bytes

Versions: 2

Compression:

Stored size: 999 Bytes

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 = {}

      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
    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

2 entries across 2 versions & 1 rubygems

Version Path
badgerfish-0.0.2 lib/badgerfish/ox_sax_parser.rb
badgerfish-0.0.1 lib/badgerfish/ox_sax_parser.rb