Sha256: c2d2b4a27b123114ce34f9030011ef94ce1cf5c84e6d7e0d9164188f37062b53
Contents?: true
Size: 1.28 KB
Versions: 7
Compression:
Stored size: 1.28 KB
Contents
# typed: true # frozen_string_literal: true require 'nokogiri' module Workato module Utilities module Xml class << self def parse_xml_to_hash(payload, strip_namespaces: false) parse_options = Nokogiri::XML::ParseOptions.new.nonet lazy_reader = Nokogiri::XML::Reader(payload, nil, nil, parse_options).to_enum.lazy lazy_reader.each_with_object([{}]) do |node, ancestors| ancestors.shift while ancestors.count > node.depth + 1 case node.node_type when Nokogiri::XML::Reader::TYPE_ELEMENT element = HashWithIndifferentAccess.new node.attributes&.each do |name, value| element["@#{strip_namespaces ? name[/(?:^xmlns:)?[^:]+$/] : name}"] = value end (ancestors.first[strip_namespaces ? node.name[/[^:]+$/] : node.name] ||= []).push(element) ancestors.unshift(element) when Nokogiri::XML::Reader::TYPE_TEXT, Nokogiri::XML::Reader::TYPE_CDATA element = ancestors.first if element.key?(:content!) element[:content!] += node.value else element[:content!] = node.value end end end.last end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems