Javascript start code from a certain line

Asked

Viewed 129 times

1

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

example of . ofx:

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 the OFXHEADER:100 until NEWFILEUID:NONE and only started working from the tag <OFX>.

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();
  • 1

    If you receive this data in a date by ajax, you could turn it into a string, capture the header and delete it?

  • 1

    You can give an example of how you use the content (in case you have already filtered the part you want)?

  • 1

    You will have to handle this manually, especially if you are using a native function to process this content, which I believe is xml... But the interesting thing would be for you to post the code, because from what I understand, you are asking if it is possible to modify the behavior of a given function, without at least specifying what function this would be.....

  • Opa, moçada. Com esse código tenho uma tabela com dois colunas mostrando o local da compra e o valor. HOWEVER, this code only works if I take out the header of the .ofx. file. My goal is for the script itself to do this, ignore the header and just start reading from the tag <OFX>.

  • 1

    @Inaldoeleuterio my question is "You can give an example of how you use the content (in case you have already filtered the part you want)?"

  • @Sergio, I’m sorry but I don’t understand. How do I use the content? Okay, come on: the information I get is from the credit card. I was able to filter by the local purchase and price tags. They are shown in a div with two columns. Only. That would be it?

Show 1 more comment

3 answers

0

    var textoPattern = /<BANKTRANLIST>.*<\/BANKTRANLIST>/;
    var objetoRegex  = new RegExp(textoPattern, 'g');
    resultado = objetoRegex.exec(reader.result.replace(/[\n\r]+/g, ''));

0

The problem is not the *.ofx file header, 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 method 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.

0

you can use a substring to remove the initial text, after this 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.