Class: MARC::XMLReader

Inherits:
Object
  • Object
show all
Defined in:
lib/enhanced_marc/xmlreader.rb

Instance Method Summary collapse

Instance Method Details

#build_recordObject

will accept parse events until a record has been built up



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/enhanced_marc/xmlreader.rb', line 5

def build_record
  record = MARC::Record.new
  data_field = nil
  control_field = nil
  subfield = nil
  text = '' 
  attrs = nil

  while @parser.has_next?
    event = @parser.pull

    if event.text?
      text += REXML::Text::unnormalize(event[0])
      next
    end

    if event.start_element?
      text = ''
      attrs = event[1]
      case strip_ns(event[0])
      when 'controlfield'
        text = ''
        control_field = MARC::ControlField.new(attrs['tag'])
      when 'datafield'
        text = ''
        data_field = MARC::DataField.new(attrs['tag'], attrs['ind1'], 
          attrs['ind2'])
      when 'subfield'
        text = ''
        subfield = MARC::Subfield.new(attrs['code'])
      end
    end

    if event.end_element?
      case strip_ns(event[0])
      when 'leader'
        record.leader = text
      when 'record'
        return record
      when 'controlfield'
        control_field.value = text
        record.append(control_field)
      when 'datafield'
        record.append(data_field)
      when 'subfield'
        subfield.value = text
        data_field.append(subfield)
      end
    end
  end
  record.to_typed_record
end