문제

require 'net/ftp'
require 'nokogiri'

server = "xxxxxx"
user = "xxxxx"
password = "xxxxx"

ftp = Net::FTP.new(server, user, password)

files = ftp.nlst('File*.xml')

files.each do |file|
   ftp.getbinaryfile(file)
   doc = Nokogiri::XML(open(file))
   # some operations with doc
end

With the code above I'm able to parse/read XML file, because it first downloads a file.

But how can I parse remote XML file without downloading it?

The code above is a part of rake task that loads rails environment when run.


UPDATE:

I'm not going to create any file. I will import info into the mongodb using mongoid.

도움이 되었습니까?

해결책

If you simply want to avoid using a temporary local file, it is possible to to fetch the file contents direct as a String, and process in memory, by supplying nil as the local file name:

files.each do |file|
   xml_string = ftp.getbinaryfile( file, nil )
   doc = Nokogiri::XML( xml_string )
   # some operations with doc
end

This still does an FTP fetch of the contents, and XML parsing happens at the client.

It is not really possible to avoid fetching the data in some form or other, and if FTP is the only protocol you have available, then that means copying data over the network using an FTP get. However, it is possible, but far more complicated, to add capabilities to your FTP (or other net-based) server, and return the data in some other form. That could include Nokogiri parsing done remotely on the server, but you'd still need to serialise the end result, fetch it and deserialise it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top