module MARC::NokogiriReader
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.
Public Class Methods
extended(receiver)
click to toggle source
# File lib/marc/xml_parsers.rb, line 97 def self.extended(receiver) require 'nokogiri' receiver.init end
Public Instance Methods
each(&block)
click to toggle source
Loop through the MARC
records in the XML document
# File lib/marc/xml_parsers.rb, line 111 def each(&block) unless block_given? return self.enum_for(:each) else @block = block @parser.parse(@handle) end end
error(evt)
click to toggle source
# File lib/marc/xml_parsers.rb, line 120 def error(evt) raise(XMLParseError, "XML parsing error: #{evt}") end
init()
click to toggle source
Sets our instance variables for SAX parsing in Nokogiri and parser
# File lib/marc/xml_parsers.rb, line 103 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
method_missing(methName, *args)
click to toggle source
# File lib/marc/xml_parsers.rb, line 125 def method_missing(methName, *args) sax_methods = [:xmldecl, :start_document, :end_document, :start_element, :end_element, :comment, :warning, :error, :cdata_block, :processing_instruction] unless sax_methods.index(methName) raise NoMethodError.new("undefined method '#{methName} for #{self}", 'no_meth') end end
Private Instance Methods
attributes_to_hash(attributes)
click to toggle source
# File lib/marc/xml_parsers.rb, line 135 def attributes_to_hash(attributes) hash = {} attributes.each do | att | hash[att.localname] = att.value end hash end