Sha256: 07ec5407b70f8a92375bd6ac628622bbd00907dc20e841058cb32537b1c7664b

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module CiviCrm
  class XML
    class << self
      def parse(text)
        # CiviCRM <Result>s sometimes contain weird elements
        # like <preferred_communication_method><0></0></ ...
        # that Nokogiri::XML can't hang with. Get rid of them before
        # parsing.
        fixed_text = text.to_s.
                     gsub("\n", "").
                     gsub(/<(\w|_)+>\s*<\d+><\/\d+>\s*<\/(\w|_)+>/, "")

        doc = Nokogiri::XML.parse(fixed_text)

        results = doc.xpath('//Result')
        results.map do |result|
          hash = {}
          result.children.each do |attribute|
            next unless attribute.is_a?(Nokogiri::XML::Element)
            hash[attribute.name] = attribute.children[0].text rescue nil
          end
          hash
        end
      end
      def encode(resources)
        builder = Nokogiri::XML::Builder.new do |xml|
          xml.ResultSet do
            Array.wrap(resources).each do |resource|
              attributes = resource.respond_to?(:attributes) ? resource.attributes : resource
              xml.Result do
                attributes.each do |key, value|
                  xml.send key.to_sym, value
                end
              end
            end
          end
        end
        builder.to_xml
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
civicrm-1.0.5 lib/civicrm/xml.rb