与REXML相关的错误

时间:2013-05-31 03:30:49

标签: ruby rexml

我不确定它是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)

它没有给出错误。

Folder / 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。这是一个问题吗?

3 个答案:

答案 0 :(得分:2)

我认为@halfelf在这里是正确的。 API docs表示Dir.foreach将遍历目录中的每个条目 - 而在Unix中,它包括两个目录...

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

答案 1 :(得分:1)

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

答案 2 :(得分: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