Read file in custom extension

Hello all,

I am busy to implement extension to convert xml to json. I made decision to use fast-xml-parser because I expect huge files (~150mb) to be imported and only this seems to be useful. Everything works fine if I do it from string in code but I have no clue how to get data from file.
I tried to use readFile() but the output is empty, no errors. Can somebody help with reading files? I tried a lot of options but nothing works…

My code for extension:

const { XMLParser } = require('fast-xml-parser');
var fs = require('fs');

exports.GetJsonXml = function () {

    fs.readFile('/public/xml/full_tst_1item.xml', 'utf8', (err, data) => {
        if (err) {
            console.error(err)
            return
        }
        var xmlDataStr = data

        // const xmlDataStr = `<product id="48" currency="PLN" code_on_card="A128-18 KHAKI" producer_code_standard="OTHER" type="regular" vat="23.0" site="4">`;

        const options = {
            ignoreAttributes: false,
            alwaysCreateTextNode: true,
            allowBooleanAttributes: true
        };

        const parser = new XMLParser(options);
        const output = parser.parse(xmlDataStr);
        return output
    })
};

Thanks in advance.

Community Page
Last updated: