I'm trying to put a ofx file inside the javascript code using input type file and show it on screen for a financial control. My problem is with getting the values separately from the ofx, be parsing it to xml or separating it in json.
I was trying at first only with javascript, but soon i realized i would be better passing it through php. Maybe i'm wrong, if there's a easy way to do it i'm open to changes.
<body>
<div class="topo">
<a style="color: white" href="">
<b>Home</b>
</a>
</div>
<div>
<div id="drop-area">
Upload OFX
<input type="file" id="upload" multiple onchange="handleFiles(this.files)">
</div>
</body>
var reader = new FileReader();
let getxml = function(path, callback){
let request = new XMLHttpRequest();
request.open("GET", path);
request.setRequestHeader("Content-Type", "text/xml");
request.onReadystatechange = function(){
if (request.readyState === 4 && request.status === 200){
callback(request.responseXML);
}
}
request.send();
}
getxml('ofxs/teste.ofx', function(xml){
console.log(xml);
})
<?php
print_r($_FILES);
$headers = array();
$charsets = array(
1252 => 'WINDOWS-1251',
);
while(!feof($_FILES)) {
$line = trim(fgets($_FILES));
if ($line === '') {
break;
}
list($header, $value) = explode(':', $line, 2);
$headers[$header] = $value;
}
$buffer = '';
// dead-cheap SGML to XML conversion
// see as well http://www.hanselman.com/blog/PostprocessingAutoClosedSGMLTagsWithTheSGMLReader.aspx
while(!feof($_FILES)) {
$line = trim(fgets($_FILES));
if ($line === '') continue;
$line = iconv($charsets[$headers['CHARSET']], 'UTF-8', $line);
if (substr($line, -1, 1) !== '>') {
list($tag) = explode('>', $line, 2);
$line .= '</' . substr($tag, 1) . '>';
}
$buffer .= $line ."\n";
}
// use DOMDocument with non-standard recover mode
$doc = new DOMDocument();
$doc->recover = true;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$save = libxml_use_internal_errors(true);
$doc->loadXML($buffer);
libxml_use_internal_errors($save);
echo $doc->saveXML();
?>
I don't really know what i'm doing , i just followed some tutos and answers from stackOV and got stuck here. '-'