Sha256: c4d47b3ca33136d9172822727cc8ba3fd24c9005cb054f13e68cafe3052e23db

Contents?: true

Size: 918 Bytes

Versions: 5

Compression:

Stored size: 918 Bytes

Contents

require "rexml/parsers/baseparser"

class Nori
  module Parser

    # = Nori::Parser::REXML
    #
    # REXML pull parser.
    module REXML

      def self.parse(xml, options)
        stack = []
        parser = ::REXML::Parsers::BaseParser.new(xml)

        while true
          event = parser.pull
          case event[0]
          when :end_document
            break
          when :end_doctype, :start_doctype
            # do nothing
          when :start_element
            stack.push Nori::XMLUtilityNode.new(options, event[1], event[2])
          when :end_element
            if stack.size > 1
              temp = stack.pop
              stack.last.add_node(temp)
            end
          when :text, :cdata
            stack.last.add_node(event[1]) unless event[1].strip.length == 0 || stack.empty?
          end
        end
        stack.length > 0 ? stack.pop.to_hash : {}
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nori-2.2.0 lib/nori/parser/rexml.rb
nori-2.1.0 lib/nori/parser/rexml.rb
nori-2.0.4 lib/nori/parser/rexml.rb
nori-2.0.3 lib/nori/parser/rexml.rb
nori-2.0.0 lib/nori/parser/rexml.rb