1

我不确定这是 REXML 还是 ruby​​ 问题。但是当我使用 REXML 时,就会发生这种情况。

下面的程序应该访问目录中每个 xml 文件的元素。

#!/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

}

当我运行时:

ruby import_xml.rb

错误:

"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>'

当我注释掉:

#xmldoc = Document.new(xmlfile)

它没有给出错误。

文件夹 /home/askar/xml_files1 仅包含 3 个 xml 文件。

我正在使用 Linux Mint Nadia 和

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

如果您注意到,由于某种原因,错误显示 ruby​​ 1.9.1。这是一个问题吗?

4

3 回答 3

2

我认为@halfelf 在这里是正确的。API 文档说这将Dir.foreach遍历目录中的每个条目——在 Unix 中,这包括两个目录....

在您调用前几行Dir.foreach,您使用 glob 构建一个名为xml_files. 如果你在循环中迭代它会发生什么?

于 2013-05-31T04:15:25.097 回答
1

只是猜测:并非所有返回的Dir.foreach(Dir.pwd)内容都是可以读取的文件。其中一些是目录。

于 2013-05-31T03:36:03.780 回答
0

使用 Nokogiri,我会这样写:

#!/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
于 2013-05-31T06:06:55.873 回答