class REXML::Element

This enables finding XML elements recursively using Ruby method syntax

Attributes

element_hash[RW]

Public Instance Methods

method_missing(method_name,*method_args) click to toggle source
# File lib/QuickBaseClient.rb, line 5130
def method_missing(method_name,*method_args)
  ret = nil 
  if elements
     if method_args and method_args.length > 0
        if method_args[0].length > 1
           searchProc = proc { |e| 
              if e.is_a?(REXML::Element) and e.name == method_name.to_s and e.attributes
                if e.attributes[method_args[0][0].to_s] and e.attributes[method_args[0][0].to_s] == method_args[0][1].to_s
                   ret = e 
                end
              end  
           }
        else  
           searchProc = proc { |e| 
              if e.is_a?(REXML::Element) and e.name == method_name.to_s and e.attributes
                if e.attributes["id"] and e.attributes["id"] == method_args[0][0].to_s
                   ret = e 
                end
                if ret.nil? and e.attributes["name"] and e.attributes["name"] == method_args[0][0].to_s
                  ret = e
                end  
              end  
           }
        end   
     else
        searchProc = proc { |e| 
           if e.is_a?(REXML::Element) and e.name == method_name.to_s 
             ret = e 
           end  
        }
     end
  end 
  processChildElements( self, false, searchProc ) 
  if ret and !ret.has_elements? and ret.has_text?
     ret = ret.text.dup 
  end
  ret
end
processChildElements( element, leafElementsOnly, block ) click to toggle source
# File lib/QuickBaseClient.rb, line 5204
def processChildElements( element, leafElementsOnly, block )
    if element
       if element.is_a?( Array )
          element.each{ |e| processChildElements( e, leafElementsOnly, block ) }
       elsif element.is_a?( REXML::Element ) and element.has_elements?
          block.call( element ) if not leafElementsOnly
          element.each{ |e|
             if e.is_a?( REXML::Element ) and e.has_elements?
               processChildElements( e, leafElementsOnly, block )
             else
               block.call( e )
             end
          }
       end
     end
end
to_h(include_element=proc{|e|true}) click to toggle source

Convert REXML Element tree into Hash. Sibling elements with duplicate names become Arrays.

# File lib/QuickBaseClient.rb, line 5171
def to_h(include_element=proc{|e|true})
  to_hash_proc = proc {|e|
     if e.is_a?(REXML::Element) and include_element.call(e)  
       e.element_hash = {}
       e.element_hash["name"] = e.name
       if e.has_attributes?
          e.element_hash["attributes"] = {}
          e.attributes.each{|k,v|e.element_hash["attributes"][k]=v}
       end
       if e.has_text?
         text = e.text.strip
         e.element_hash["value"] = text if text.length > 0
       end
       if e.parent and e.parent.is_a?(REXML::Element)
         if e.parent.element_hash and e.parent.element_hash[e.name]
            if e.parent.element_hash[e.name].is_a?(Array)
              e.parent.element_hash[e.name] << e.element_hash
            elsif e.parent.element_hash
              tmp = e.parent.element_hash[e.name].dup
              e.parent.element_hash[e.name] = []
              e.parent.element_hash[e.name] << tmp
              e.parent.element_hash[e.name] << e.element_hash
            end
         elsif e.parent.element_hash
           e.parent.element_hash[e.name] = e.element_hash
         end
       end
     end
  }
  processChildElements( self, false, to_hash_proc )
  element_hash
end