Sha256: e90281dd663a373bef298cef1b3780832ed4526434444a43cf77930d60f66093

Contents?: true

Size: 933 Bytes

Versions: 5

Compression:

Stored size: 933 Bytes

Contents

require "rexml/parsers/baseparser"
require "nori/xml_utility_node"

module Nori
  module Parser

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

      def self.parse(xml)
        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(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-1.0.0 lib/nori/parser/rexml.rb
nori-0.2.3 lib/nori/parser/rexml.rb
nori-0.2.2 lib/nori/parser/rexml.rb
nori-0.2.1 lib/nori/parser/rexml.rb
nori-0.2.0 lib/nori/parser/rexml.rb