module XML

Public Class Methods

load_attributes(dirpath,xpath) { |attributes| ... } click to toggle source

Sugar to load attributes from a file.

@example

XML.load_attributes('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
# File lib/sixarm_ruby_xml_load.rb, line 56
def XML.load_attributes(dirpath,xpath)
  XML.load_elements(dirpath,xpath){|elem|
    yield elem.attributes
  }
end
load_attributes_array(dirpath,xpath) { |attributes.to_a| ... } click to toggle source

Sugar to load attributes array from a file.

@example

XML.load_attributes_array('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
# File lib/sixarm_ruby_xml_load.rb, line 68
def XML.load_attributes_array(dirpath,xpath)
  XML.load_elements(dirpath,xpath){|elem|
    yield elem.attributes.to_a
  }
end
load_attributes_hash(dirpath,xpath) { |attributes.to_a_hash| ... } click to toggle source

Sugar to load attributes hash from a file.

@example

XML.load_attributes_hash('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
# File lib/sixarm_ruby_xml_load.rb, line 80
def XML.load_attributes_hash(dirpath,xpath)
  XML.load_elements(dirpath,xpath){|elem|
    yield elem.attributes.to_a_hash
  }
end
load_dir(*dirpaths) { |doc| ... } click to toggle source

Specify one or more directory patterns and pass each XML file in the matching directories to a block.

See [Dir#glob](www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.

@example To load xml documents

XML.load_dir('/tmp/*.xml'){|xml_document|
  #...whatever you want to do with each xml document
}

@example To load xml documents in files beginning in “foo” or “bar”

XML.load_dir('/tmp/foo*.yaml','/tmp/bar*.xml','){|xml_document|
  #...whatever you want to do with the xml document
}
# File lib/sixarm_ruby_xml_load.rb, line 24
def XML.load_dir(*dirpaths)
  dirpaths=[*dirpaths.flatten]
  dirpaths.each do |dirpath|
    Dir[dirpath].sort.each do |filename|
      File.open(filename) do |file|
        doc = REXML::Document.new file
        yield doc
      end #file
    end #dir
  end #each
end
load_elements(dirpath,xpath) { |elem| ... } click to toggle source

Sugar to load elements from a file.

@example

XML.load_elements('config.xml','userlist/user'){|element| pp element.attributes['first_name'] }
# File lib/sixarm_ruby_xml_load.rb, line 42
def XML.load_elements(dirpath,xpath)
  XML.load_dir(dirpath){|doc|
    doc.elements.each(xpath){|elem|
      yield elem
    }
  }
end