title: Ruby: Convert big .xml files to csv
body: <p>I have a little problem.
  I need to convert large xml files (1-4GB each) to CSV.
  I know that I can do this with Nokogiri's SAX parser, but I'm stuck.</p>
  
  <pre><code>&lt;Documents&gt;
      &lt;Document DocID="10170306" DocType="Message"&gt;
      &lt;FieldValues&gt;
  
          &lt;E03&gt;-1166737392&lt;/E03&gt;
          &lt;E05&gt;petrosky (petrosky@foo.com)&lt;/E05&gt;
          &lt;E06&gt;00000000B89476181EE6C34FB4E9D87F9E44A85944002000&lt;/E06&gt;
          &lt;E07&gt;\foo-dedup-global.mbox_99.mbox\&lt;/E07&gt;
          &lt;E08&gt;5/12/2011 6:32:38 PM&lt;/E08&gt;
          &lt;E09&gt;Fwd: important decision for v1 launch&lt;/E09&gt;
          &lt;E10&gt;Susan Infantino (susani@foo.com); Mike Yang (foo@mail.com)&lt;/E10&gt;
          &lt;F01&gt;Jun  8 2011  7:43AM&lt;/F01&gt;
          &lt;F02&gt;May 12 2011  6:32PM&lt;/F02&gt;
          &lt;F03&gt;Msg0002_important decisi.html&lt;/F03&gt;
          &lt;F04&gt;MSMAIL&lt;/F04&gt;
          &lt;F05&gt;CA4DBB95C638FB656CB02627DDEA90C9&lt;/F05&gt;
          &lt;F06&gt;28677&lt;/F06&gt;
          &lt;F07&gt;foo-dedup-global.mbox_99.mbox.pst&lt;/F07&gt;
          &lt;F08&gt;10164846&lt;/F08&gt;
          &lt;F09&gt;10170306&lt;/F09&gt;
          &lt;E11&gt;0&lt;/E11&gt;
          &lt;E12&gt;&amp;lt;BANLkTi=yztN5Pd0v9i9+zN=aYhAo5Y8ffA@mail.foo.com&amp;gt;&lt;/E12&gt;
  
      &lt;/FieldValues&gt;
      &lt;Files&gt;
        &lt;File FileType="Native"&gt;
          &lt;ExternalFile FilePath="\04_EXT\31\foo-dedup-global.mbox_99.mbox.pst10164846.dir\foo-dedup-global.mbox_99.mbox\" FileName="Msg0002_important decisi.html" FileSize="28677" Hash="CA4DBB95C638FB656CB02627DDEA90C9" HashType="MD5" /&gt;
        &lt;/File&gt;
      &lt;/Files&gt;
      &lt;Locations&gt;
        &lt;Location&gt;
          &lt;Custodian&gt;Yang_Mike&lt;/Custodian&gt;
          &lt;LocationURI&gt;\\ANNATXCIFS02\PN_Dunbar_F01401\04_EXT\31\foo-dedup-global.mbox_99.mbox.pst10164846.dir\foo-dedup-global.mbox_99.mbox\Msg0002_important decisi.html&lt;/LocationURI&gt;
        &lt;/Location&gt;
      &lt;/Locations&gt;    
    &lt;/Document&gt;
  &lt;/Documents&gt;
  </code></pre>
  
  <p>I played a littlebit around with event driven programming.</p>
  
  <pre><code>require 'fileutils'
  require 'faster_csv'
  require 'nokogiri'
  
  file = ARGV[0]
  
  include Nokogiri
  
  class Xmlfile &lt; XML::SAX::Document
    def start_element name, attrs
      # Process data here
      if name == 'Document'
        documentName = [*attrs]
        puts documentName
      end     
      if name == 'File'
        file = [*attrs]
        puts file
      end
      if name == 'ExternalFile'
        externalFile = [*attrs]
        puts externalFile 
      end
    end
  
  #   def end_element name, attrs
  #   end
  
  end
  
  parser = XML::SAX::Parser.new(Xmlfile.new)
  parser.parse_file(file)
  </code></pre>