简体   繁体   English

如何使用nokogiri gem从FTP远程解析XML文件,无需下载

[英]how to parse XML file remotely from FTP with nokogiri gem, without downloading

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. 使用上面的代码,我能够解析/读取XML文件,因为它首先下载文件。

But how can I parse remote XML file without downloading it? 但是,如何在不下载的情况下解析远程XML文件呢?

The code above is a part of rake task that loads rails environment when run. 上面的代码是rake任务的一部分,它在运行时加载rails环境。


UPDATE: 更新:

I'm not going to create any file. 我不会创建任何文件。 I will import info into the mongodb using mongoid. 我将使用mongoid将信息导入mongodb。

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: 如果您只是想避免使用临时本地文件,则可以通过提供nil作为本地文件名来直接以String形式获取文件内容并在内存中进行处理:

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. 这仍然会通过FTP来获取内容,并且XML解析会在客户端进行。

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 . 确实不可能避免以某种形式获取数据,并且如果FTP是您唯一可用的协议,则意味着使用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. 但是,可以(但更为复杂)向FTP(或其他基于网络的)服务器添加功能,并以其他形式返回数据。 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. 这可能包括在服务器上远程完成的Nokogiri解析,但是您仍然需要对最终结果进行序列化,获取并反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM