Tutorial :Ruby Load multiple xml from a directory in a program to parse them



Question:

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


Solution:1

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »