Askar
Askar

Reputation: 5854

error related to REXML

I'm not sure it's REXML or ruby issue. But this is happening when I work with REXML.

The program below should access elements of each xml file in the directory.

#!/usr/bin/ruby -w

require 'rexml/document'
include REXML

p "Current directory was: " + Dir.pwd

Dir.chdir("/home/askar/xml_files1") {

    p "Now we're in: " + Dir.pwd

    if File.exist?(Dir.pwd)

        xml_files = Dir.glob("ShipmentRequest*.xml")

        Dir.foreach(Dir.pwd) do |file|

            xmlfile = File.new(file)
            xmldoc = Document.new(xmlfile)

        end

    else
        puts "It's empty"
    end

}

When I run:

ruby import_xml.rb

Errors:

"Current directory was: /home/askar/Dropbox/rails_studio/xml_to_mysql"
"Now we're in: /home/askar/xml_files1"
There're 6226 files in the folder...
/home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/source.rb:148:in `read': Is a directory - . (Errno::EISDIR)
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/source.rb:148:in `initialize'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/source.rb:14:in `new'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/source.rb:14:in `create_from'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/parsers/baseparser.rb:127:in `stream='
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/parsers/baseparser.rb:116:in `initialize'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/parsers/treeparser.rb:9:in `new'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/parsers/treeparser.rb:9:in `initialize'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/document.rb:245:in `new'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/document.rb:245:in `build'
    from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/rexml/document.rb:43:in `initialize'
    from import_xml.rb:20:in `new'
    from import_xml.rb:20:in `block (2 levels) in <main>'
    from import_xml.rb:17:in `foreach'
    from import_xml.rb:17:in `block in <main>'
    from import_xml.rb:8:in `chdir'
    from import_xml.rb:8:in `<main>'

When I comment out:

#xmldoc = Document.new(xmlfile)

it's not giving errors.

Folder /home/askar/xml_files1 contains only 3 xml files.

I'm using Linux Mint Nadia and

ruby -v
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]

If you noticed, for some reason, error shows ruby 1.9.1. Is this an issue?

Upvotes: 1

Views: 429

Answers (3)

the Tin Man
the Tin Man

Reputation: 160571

Using Nokogiri, here's how I'd write this:

#!/usr/bin/ruby -w

require 'nokogiri'

DIRNAME = "/home/askar/xml_files1"

puts "Current directory is: #{ Dir.pwd }"
Dir.chdir(DIRNAME) do

  puts "Now in: #{ DIRNAME }"
  xml_files = Dir.glob("ShipmentRequest*.xml")

  if xml_files.empty?
    puts "#{ DIRNAME } is empty."
  else
    xml_files.each do |file|
      doc = Nokogiri::XML(open(file))
      # ... do something with the doc ...
    end
  end
end

Upvotes: 0

dpassage
dpassage

Reputation: 5453

I think @halfelf is correct here. The API docs say that Dir.foreach will iterate over every entry in the directory - and in Unix, that includes the two directories . and ...

A couple lines before your Dir.foreach call, you use glob to build an array of files called xml_files. What happens if you iterate over that in your loop instead?

Upvotes: 2

halfelf
halfelf

Reputation: 10107

Just a guess: Not everything returned by Dir.foreach(Dir.pwd) is a file that can be read. Some of them are directories.

Upvotes: 1

Related Questions