Sha256: 86f5205c642577767aa5678684f448343421c408629da57928cacd6d4ff11cc7

Contents?: true

Size: 947 Bytes

Versions: 6

Compression:

Stored size: 947 Bytes

Contents

# frozen_string_literal: true

module Awis
  module Utils
    class XML
      attr_reader :data

      def initialize(data)
        @data = data
      end

      def each_node(attributes_in_path = false)
        reader = Nokogiri::XML::Reader(@data)
        nodes = ['']

        reader.each do |node|
          if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
            if attributes_in_path && !node.attributes.empty?
              attributes = []

              node.attributes.sort.each do |name, value|
                attributes << "@#{name}=#{value}"
              end
              nodes << "#{node.name}/#{attributes.join('/')}"
            else
              nodes << node.name
            end
            path = nodes.join('/')

            yield node, path
          end

          nodes.pop if node.node_type == Nokogiri::XML::Reader::TYPE_END_ELEMENT || node.self_closing? # End tag
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
awis-sdk-ruby-2.1.0 lib/awis/utils/xml.rb
awis-sdk-ruby-2.0.0 lib/awis/utils/xml.rb
awis-sdk-ruby-1.1.2 lib/awis/utils/xml.rb
awis-sdk-ruby_-1.1.1.1 lib/awis/utils/xml.rb
awis-sdk-ruby_-1.1.1p1 lib/awis/utils/xml.rb
awis-sdk-ruby-1.1.1 lib/awis/utils/xml.rb