Javascript start the code from a certain line

1

I have a code that runs through an .ofx file for reading its markup. However, the .ofx code has a header that makes it impossible to read (that is, for the javascript code to work I have to remove the header manually). Is there any way to set the line to which javascript will start a function?

.ofx example:

OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>

I wanted my code to ignore from OFXHEADER:100 to NEWFILEUID:NONE and just started working from the <OFX> tag.

I'm using this code to capture the tags I want:

function loadOFXDoc() {
  var reader = new XMLHttpRequest();
  reader.onreadystatechange = function() {
    if (reader.readyState == 4 && reader.status == 200) {
      showContent(reader);
    }
  };
  reader.open("GET", "sample.ofx", true);
  reader.send();
}
function showContent(xml) {
  var i;
  var ofxDoc = xml.responseXML;
  var table="<tr><th>Local</th><th>Valor</th></tr>";
  var x = ofxDoc.getElementsByTagName("STMTTRN");
  for (i = 0; i <x.length; i++) {
    table += "<tr><td>" +
    x[i].getElementsByTagName("MEMO")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("TRNAMT")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}

loadOFXDoc();
    
asked by anonymous 31.07.2016 / 00:33

3 answers

0

The problem is not the header of the * .ofx file, it is because the file does not have the XML header. If the XML header is not declared in the file, it is not interpreted.

According to this page you can use the overrideMimeType to force the file to be interpreted as XML, without having to declare the XML header.

reader.overrideMimeType('text/xml');
reader.send();

Optional :

Another solution is also to use an XML interpreter to interpret the normal text of the file. I wrote a XML interpreter to answer your question.

    
01.08.2016 / 16:28
0

You can use a substring to remove the initial text, then just do a parse in the document.

var data = document.querySelector("template").innerHTML;
var blob = new Blob([data], { type: "application/x-ofx" });
var _url = URL.createObjectURL(blob);

var http = new XMLHttpRequest();
http.open("GET", _url, true);
http.addEventListener("readystatechange", function (event) {
  if (http.readyState == 4) {
    var index = http.responseText.indexOf("<ofx>");
    var _text = http.responseText.substring(index);
    
    var parser = new DOMParser();
    var ofxDoc = parser.parseFromString(_text, "text/xml");
    
    // realizando uma busca simples no documento ofx.
    var severity = ofxDoc.querySelector("ofx sonrs status severity");
    console.log(severity.textContent);
  }
});
http.send();
<template>
  OFXHEADER:100
  DATA:OFXSGML
  VERSION:102
  SECURITY:NONE
  ENCODING:USASCII
  CHARSET:1252
  COMPRESSION:NONE
  OLDFILEUID:NONE
  NEWFILEUID:NONE
  <OFX>
    <SIGNONMSGSRSV1>
      <SONRS>
        <STATUS>
          <CODE>0</CODE>
          <SEVERITY>INFO</SEVERITY>
        </STATUS>
      </SONRS>
    </SIGNONMSGSRSV1>
  </OFX>
</template>
    
01.08.2016 / 19:54
0
    var textoPattern = /<BANKTRANLIST>.*<\/BANKTRANLIST>/;
    var objetoRegex  = new RegExp(textoPattern, 'g');
    resultado = objetoRegex.exec(reader.result.replace(/[\n\r]+/g, ''));
    
10.07.2017 / 16:30