title: How to parse this OFX file?
body: <p>this is an original ofx file as it comes from m bank (no worries, theres nothing sensitive, i cut out the middle part with all the transactions)</p>
  
  <blockquote>
    <p>Open Financial Exchange (OFX) is a
    data-stream format for exchanging
    financial information that evolved
    from Microsoft's Open Financial
    Connectivity (OFC) and Intuit's Open
    Exchange file formats.</p>
  </blockquote>
  
  <p>now i need to parse this. i already saw that <a href="https://stackoverflow.com/questions/2221608/how-to-parse-a-malformed-xml-ofx-with-ofx4j">question</a>, but this is not a dup because i am interested in how to do this.</p>
  
  <p>i am sure i could figure out some clever regexps that would do the job, but that is ugly and error vulnerable (if the format is changed, some fields may be missing, the formatting/white spaces are different etc etc...)</p>
  
  <pre><code>OFXHEADER:100
  DATA:OFXSGML
  VERSION:102
  SECURITY:NONE
  ENCODING:USASCII
  CHARSET:1252
  COMPRESSION:NONE
  OLDFILEUID:NONE
  NEWFILEUID:NONE
  &lt;OFX&gt;
      &lt;SIGNONMSGSRSV1&gt;
          &lt;SONRS&gt;
              &lt;STATUS&gt;
                  &lt;CODE&gt;0
                  &lt;SEVERITY&gt;INFO
              &lt;/STATUS&gt;
              &lt;DTSERVER&gt;20110420000000[+1:CET]
              &lt;LANGUAGE&gt;ENG
          &lt;/SONRS&gt;
      &lt;/SIGNONMSGSRSV1&gt;
      &lt;BANKMSGSRSV1&gt;
          &lt;STMTTRNRS&gt;
              &lt;TRNUID&gt;1
              &lt;STATUS&gt;
                  &lt;CODE&gt;0
                  &lt;SEVERITY&gt;INFO
              &lt;/STATUS&gt;
              &lt;STMTRS&gt;
                  &lt;CURDEF&gt;EUR
                  &lt;BANKACCTFROM&gt;
                      &lt;BANKID&gt;20404
                      &lt;ACCTID&gt;02608983629
                      &lt;ACCTTYPE&gt;CHECKING
                  &lt;/BANKACCTFROM&gt;
                      &lt;BANKTRANLIST&gt;
                      &lt;DTSTART&gt;20110207
                      &lt;DTEND&gt;20110419
                      &lt;STMTTRN&gt;
                          &lt;TRNTYPE&gt;XFER
                          &lt;DTPOSTED&gt;20110205000000[+1:CET]
                          &lt;TRNAMT&gt;-6.12
                          &lt;FITID&gt;C74BD430D5FF2521
                          &lt;NAME&gt;unbekannt
                          &lt;MEMO&gt;BILLA DANKT  1265P K2 05.02.UM 17.49 
                      &lt;/STMTTRN&gt;
                      &lt;STMTTRN&gt;
                          &lt;TRNTYPE&gt;XFER
                          &lt;DTPOSTED&gt;20110207000000[+1:CET]
                          &lt;TRNAMT&gt;-10.00
                          &lt;FITID&gt;C74BE0F90A657901
                          &lt;NAME&gt;unbekannt
                          &lt;MEMO&gt;AUTOMAT  13177 KARTE2 07.02.UM 10:22 
                      &lt;/STMTTRN&gt;
  ............................. goes on like this ........................
                      &lt;STMTTRN&gt;
                          &lt;TRNTYPE&gt;XFER
                          &lt;DTPOSTED&gt;20110418000000[+1:CET]
                          &lt;TRNAMT&gt;-9.45
                          &lt;FITID&gt;C7A5071492D14D29
                          &lt;NAME&gt;unbekannt
                          &lt;MEMO&gt;HOFER DANKT  0408P K2 18.04.UM 18.47 
                      &lt;/STMTTRN&gt;
                  &lt;/BANKTRANLIST&gt;
                  &lt;LEDGERBAL&gt;
                      &lt;BALAMT&gt;1992.29
                      &lt;DTASOF&gt;20110420000000[+1:CET]
                  &lt;/LEDGERBAL&gt;
              &lt;/STMTRS&gt;
          &lt;/STMTTRNRS&gt;
      &lt;/BANKMSGSRSV1&gt;
  &lt;/OFX&gt;
  </code></pre>
  
  <p>i currently use this code which gives me the desired result:</p>
  
  <pre><code>&lt;?
  
  $files = array();
  $files[] = '***_2011001.ofx';
  $files[] = '***_2011002.ofx';
  $files[] = '***_2011003.ofx';
  
  system('touch file.csv &amp;&amp; chmod 777 file.csv');
  $fp = fopen('file.csv', 'w');
  
  foreach($files as $file) {
      echo $file."...\n";
      $content = file_get_contents($file);
  
      $content = str_replace("\n","",$content);
      $content = str_replace(" ","",$content);
  
      $regex = '|&lt;STMTTRN&gt;&lt;TRNTYPE&gt;(.+?)&lt;DTPOSTED&gt;(.+?)&lt;TRNAMT&gt;(.+?)&lt;FITID&gt;(.+?)&lt;NAME&gt;(.+?)&lt;MEMO&gt;(.+?)&lt;/STMTTRN&gt;|';
  
  
      echo preg_match_all($regex,$content,$matches,PREG_SET_ORDER)." matches... \n";
  
  
      foreach($matches as $match) {
          echo ".";
          array_shift($match);
          fputcsv($fp, $match);
      }
      echo "\n";
  }
  echo "done.\n";
  fclose($fp);
  </code></pre>
  
  <p>this is really ugly and if this was a valid xml file i would personally kill myself for that, but how to do it better?</p>