Module: MARC::LibXMLReader

Defined in:
lib/marc/xml_parsers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(receiver) ⇒ Object



298
299
300
301
# File 'lib/marc/xml_parsers.rb', line 298

def self.extended(receiver)
  require 'xml'
  receiver.init
end

Instance Method Details

#build_recordObject

each



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/marc/xml_parsers.rb', line 316

def build_record
  r = MARC::Record.new()
    until (@parser.local_name == 'record' and @parser.node_type == XML::Reader::TYPE_END_ELEMENT) do
      @parser.read
      next if @parser.node_type == XML::Reader::TYPE_END_ELEMENT
      case @parser.local_name
      when 'leader'
       @parser.read
       r.leader = @parser.value
      when 'controlfield'
       tag = @parser['tag']
       @parser.read
       r << MARC::ControlField.new(tag, @parser.value)
      when 'datafield'
       data = MARC::DataField.new(@parser['tag'], @parser['ind1'], @parser['ind2'])
       while (@parser.read and !(@parser.local_name == 'datafield' and @parser.node_type == XML::Reader::TYPE_END_ELEMENT)) do
         next if @parser.node_type == XML::Reader::TYPE_END_ELEMENT
         case @parser.local_name
         when 'subfield'
           code =@parser['code']
           @parser.read
           data.append(MARC::Subfield.new(code, @parser.value))
         end
       end
       r << data

      end # case
    end #until
  return r
end

#eachObject



308
309
310
311
312
313
314
# File 'lib/marc/xml_parsers.rb', line 308

def each
  while (@parser.read) do
   if @parser.local_name == 'record' && @parser.namespace_uri == @ns
     yield build_record
   end
  end # while
end

#initObject



303
304
305
306
# File 'lib/marc/xml_parsers.rb', line 303

def init
  @ns = "http://www.loc.gov/MARC21/slim"
  @parser = XML::Reader.io(@handle)
end