| 12
 3
 4
 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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 
 | require 'xml'
 
XML_FILE = './exported_0.xml'
 
# A special file class that we'll write its output in
# successive files with the same number of items in it
# (except for the last one which size may vary)
class SplitFile
  def initialize(template_name, items_per_file)
    @template_name = template_name
    @items_per_file = items_per_file
    @item_count = 0
    @file_index = 0
    @fh = open()
  end
 
  def write(content)
    @fh.write(content)
    @fh.write("\n")
    @item_count += 1
    if @item_count == @items_per_file
      self.close()
      self.open()
    end
  end
 
  def open
    exit if @file_index == 3
    @file_index += 1
    @fh = File.open(self.batch_name(), 'w')
  end
 
  def close
    @fh.close
    @item_count = 0
  end
 
  def batch_name
    @template_name % @file_index
  end
end
 
# XML Processor to split the Product records into
# smaller XML chunks
class XMLSplitProcessor
 
  def initialize(xml_sourcefile, split_filename_template)
    @split_filename_template = split_filename_template
    @file_handle = SplitFile.new(split_filename_template, 1000)
    @reader = XML::Reader.file(xml_sourcefile)
  end
 
  def run
    while @reader.read do
      self.process_node()
    end
  end
 
  def process_node
    if @reader.name == 'Product' && reader.node_type == XML::Reader::TYPE_ELEMENT
      @file_handle.write(@reader.read_outer_xml)
      @reader.next
    end
  end
 
end
 
# Main
XMLSplitProcessor.new(XML_FILE, "batch_%s.xml").run | 
Partager