tags:

views:

26

answers:

1

Hi

I want to load a set of xml from a directory and use REXML to parse all the xml in a loop. I cant seem to create File Object after i start reading from a directory

      i=1
     filearray=Array.new
        documentarray=Array.new
        directory = 'xml'
        Dir.foreach(directory).each { |file| 
         next if file == '.' or file == '..'
         filearray[i]=File.open(directory +"/"+file)
       i=i+1

Please help

A: 

You are opening the file, but not reading it. This is ugly, but will work:

require 'find'

files = []
directory = 'xml'

def get_contents(file)
  contents = ""
  contents = File.open(file).readlines
end

Find.find(directory) do |file|
  next if FileTest.directory?(file)
  files << get_contents(file)
end

Hope it helps

Brian