Sha256: 3644897df49e814afb15d54e9ddd5e0be42ae5ad863d228f2de15b58966fd09e

Contents?: true

Size: 1.92 KB

Versions: 44

Compression:

Stored size: 1.92 KB

Contents

module MultiXml
  module Parsers
    module Libxml2Parser #:nodoc:
      # Convert XML document to hash
      #
      # node::
      #   The XML node object to convert to a hash.
      #
      # hash::
      #   Hash to merge the converted element into.
      def node_to_hash(node, hash={})
        node_hash = {MultiXml::CONTENT_ROOT => ''}

        name = node_name(node)

        # Insert node hash into parent hash correctly.
        case hash[name]
          when Array then hash[name] << node_hash
          when Hash  then hash[name] = [hash[name], node_hash]
          when nil   then hash[name] = node_hash
        end

        # Handle child elements
        each_child(node) do |c|
          if c.element?
            node_to_hash(c, node_hash)
          elsif c.text? || c.cdata?
            node_hash[MultiXml::CONTENT_ROOT] << c.content
          end
        end

        # Remove content node if it is empty
        if node_hash[MultiXml::CONTENT_ROOT].strip.empty?
          node_hash.delete(MultiXml::CONTENT_ROOT)
        end

        # Handle attributes
        each_attr(node) do |a|
          key = node_name(a)

          node_hash[key] = if v = node_hash[key]
                             [a.value, v]
                           else
                             a.value
                           end
        end

        hash
      end

      # Parse an XML Document IO into a simple hash.
      # xml::
      #   XML Document IO to parse
      def parse(xml)
        raise NotImplementedError, "inheritor should define #{__method__}"
      end

      # :stopdoc:
      private

      def each_child(*args)
        raise NotImplementedError, "inheritor should define #{__method__}"
      end

      def each_attr(*args)
        raise NotImplementedError, "inheritor should define #{__method__}"
      end

      def node_name(*args)
        raise NotImplementedError, "inheritor should define #{__method__}"
      end
    end
  end
end

Version data entries

44 entries across 43 versions & 6 rubygems

Version Path
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/1.9.1/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.9 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.8 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.7 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.6 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.5 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.4 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.3 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.2 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
allegro_api_client-0.0.1 gems/ruby/2.1.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.3.1 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.3.0 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.2.9 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.2.8 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.2.7 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.2.6 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb
plyom_user-0.2.5 vendor/bundle/ruby/2.0.0/gems/multi_xml-0.5.5/lib/multi_xml/parsers/libxml2_parser.rb