NokogiriReader uses the Nokogiri SAX Parser to quickly read a MARCXML document. Because dynamically subclassing MARC::XMLReader is a little ugly, we need to recreate all of the SAX event methods from Nokogiri::XML::SAX::Document here rather than subclassing.
# File lib/marc/xml_parsers.rb, line 93 def self.extended(receiver) require 'nokogiri' receiver.init end
Loop through the MARC records in the XML document
# File lib/marc/xml_parsers.rb, line 107 def each(&block) @block = block @parser.parse(@handle) end
Sets our instance variables for SAX parsing in Nokogiri and parser
# File lib/marc/xml_parsers.rb, line 99 def init @record = {:record=>nil,:field=>nil,:subfield=>nil} @current_element = nil @ns = "http://www.loc.gov/MARC21/slim" @parser = Nokogiri::XML::SAX::Parser.new(self) end
# File lib/marc/xml_parsers.rb, line 113 def method_missing(methName, *args) sax_methods = [:xmldecl, :start_document, :end_document, :start_element, :end_element, :comment, :warning, :error, :cdata_block] unless sax_methods.index(methName) raise NoMethodError.new("undefined method '#{methName} for #{self}", 'no_meth') end end
# File lib/marc/xml_parsers.rb, line 123 def attributes_to_hash(attributes) hash = {} attributes.each do | att | hash[att.localname] = att.value end hash end