使用SAX Parser获取多个子节点?

我有一个大型本地XML文件(24 GB),其结构如下:

****  **** (several times within an id...) 

我需要这样的结果:

 id1;url1 id1;url2 id1;url3 id2;url4 .... 

我想将Nokigiri与SAX Parser或Reader一起使用,因为我无法将整个文件加载到内存中。 我正在使用Ruby Rake任务来执行代码。

我的SAX代码是:

 task :fetch_saxxml => :environment do require 'nokogiri' require 'open-uri' class MyDocument < Nokogiri::XML::SAX::Document attr_accessor :is_name def initialize @is_name = false end def start_element name, attributes = [] @is_name = name.eql?("id") end def characters string string.strip! if @is_name and !string.empty? puts "ID: #{string}" end end def end_document puts "the document has ended" end end parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new) parser.parse_file('/path_to_my_file.xml') end 

这很好,以便获取文件中的ID,但我也需要获取每个id节点中的URL。

如何在该代码中添加“each do”之类的内容来获取URL并获得如上所示的输出? 或者是否可以在“字符”中调用多个操作?

实际上,这是解析几个节点发生时的解决方案。 SAX解析器的问题在于你必须找到一种方法来处理像“&”之类的特殊字符……但这是另一个故事。

这是我的代码:

 class MyDoc < Nokogiri::XML::SAX::Document def start_element name, attrs = [] @inside_content = true if name == 'yourvalue' @current_element = name end def characters str if @current_element == 'your_1st subnode' elsif @current_element == 'your 2nd subnode' end puts "#{@current_element} - #{str}" if @inside_content && %w{your_subnodes here}.include?(@current_element) end def end_element name @inside_content = false if name == 'yourvalue' @current_element = nil end end parser = Nokogiri::XML::SAX::Parser.new(MyDoc.new) parser.parse_file('/path_to_your.xml') end