Skip to content

Commit

Permalink
Add support for QFX files with bodies already in XML format (not need…
Browse files Browse the repository at this point in the history
…ing SGML mangling)
  • Loading branch information
webmonarch committed Jan 5, 2017
1 parent 4b00828 commit 30667e3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
31 changes: 22 additions & 9 deletions ofx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
var xml2json = require('xml2json');

function sgml2Xml(sgml) {
return sgml
.replace(/>\s+</g, '><') // remove whitespace inbetween tag close/open
.replace(/\s+</g, '<') // remove whitespace before a close tag
.replace(/>\s+/g, '>') // remove whitespace after a close tag
.replace(/<([A-Z0-9_]*)+\.+([A-Z0-9_]*)>([^<]+)/g, '<\$1\$2>\$3' )
.replace(/<(\w+?)>([^<]+)/g, '<\$1>\$2</\$1>');
}

function parseXml(content) {
return JSON.parse(xml2json.toJson(content, { coerce: false }))
}

function parse(data) {
// firstly, split into the header attributes and the footer sgml
var ofx = data.split('<OFX>', 2);
Expand All @@ -13,16 +26,16 @@ function parse(data) {
});

// make the SGML and the XML
var sgml = '<OFX>' + ofx[1];
var xml = sgml
.replace(/>\s+</g, '><')
.replace(/\s+</g, '<')
.replace(/>\s+/g, '>')
.replace(/<([A-Z0-9_]*)+\.+([A-Z0-9_]*)>([^<]+)/g, '<\$1\$2>\$3' )
.replace(/<(\w+?)>([^<]+)/g, '<\$1>\$2</\$1>');
var content = '<OFX>' + ofx[1];

// parse the XML
var data = JSON.parse(xml2json.toJson(xml, { coerce: false }));
// Parse the XML/SGML portion of the file into an object
// Try as XML first, and if that fails do the SGML->XML mangling
var data = null;
try {
data = parseXml(content);
} catch (e) {
data = parseXml(sgml2Xml(content));
}

// put the headers into the returned data
data.header = header;
Expand Down
8 changes: 8 additions & 0 deletions test/data/example-xml.qfx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
<OFX><SIGNONMSGSRSV1><SONRS><STATUS><CODE>0</CODE><SEVERITY>INFO</SEVERITY></STATUS><DTSERVER>20170104225053</DTSERVER><LANGUAGE>ENG</LANGUAGE><FI><ORG>Ally</ORG><FID>6157</FID></FI><INTU.BID>6157</INTU.BID><INTU.USERID>XXXXXXXXXX</INTU.USERID></SONRS></SIGNONMSGSRSV1><BANKMSGSRSV1><STMTTRNRS><TRNUID>0</TRNUID><STATUS><CODE>0</CODE><SEVERITY>Info</SEVERITY></STATUS><STMTRS><CURDEF>USD</CURDEF><BANKACCTFROM><BANKID>XXXXXXXXX</BANKID><ACCTID>XXXXXXXXXX</ACCTID><ACCTTYPE>SAVINGS</ACCTTYPE></BANKACCTFROM><BANKTRANLIST><DTSTART>20161204000000</DTSTART><DTEND>20170104000000</DTEND><STMTTRN><TRNTYPE>INT</TRNTYPE><DTPOSTED>20161215000550</DTPOSTED><TRNAMT>0.84</TRNAMT><FITID>21172131531687</FITID><NAME>Interest Paid</NAME></STMTTRN></BANKTRANLIST><LEDGERBAL><BALAMT>1025.23</BALAMT><DTASOF>20170104225053</DTASOF></LEDGERBAL><AVAILBAL><BALAMT>1025.23</BALAMT><DTASOF>20170104225053</DTASOF></AVAILBAL></STMTRS></STMTTRNRS></BANKMSGSRSV1></OFX>
24 changes: 24 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ test('parse', function (t) {
t.end();
});

test('parse XML', function (t) {
var file = fs.readFileSync(__dirname + '/data/example-xml.qfx', 'utf8');
var data = ofx.parse(file);

// headers
t.equal(data.header.OFXHEADER, '100');
t.equal(data.header.ENCODING, 'USASCII');

var transaction = data.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST.STMTTRN;
t.same(transaction, {
TRNTYPE: 'INT',
DTPOSTED: '20161215000550',
TRNAMT: '0.84',
FITID: '21172131531687',
NAME: 'Interest Paid'
});

var status = data.OFX.SIGNONMSGSRSV1.SONRS.STATUS;
t.equal(status.CODE, '0');
t.equal(status.SEVERITY, 'INFO');

t.end();
})

// parse <-> serialize
test('serialize', function(t) {
var expected = fs.readFileSync(__dirname + '/data/example1.ofx', 'utf8');
Expand Down

0 comments on commit 30667e3

Please sign in to comment.