1

I have a 1.7GB XML file and I want to convert it into a JSON file. When I use "xml2json" module to parse it, it returns

Error: There are errors in your xml file: out of memory

My code is:

const fs = require("fs");
const parser = require("xml2json");

const xmlObject = fs.readFileSync("./database1.xml")
const tempObject = JSON.parse(parser.toJson(xmlObject));

And this is the error:

throw new Error('There are errors in your xml file: ' + parser.getError());
    ^

Error: There are errors in your xml file: out of memory

I already add the parameter "--max-old-space-size=4096" when running the code but it doesn't work.

Is there any solution? Or could someone provide another way to convert a large XML into JSON file? Thanks.

1
  • For this, you should use node-expat because its using streaming which would reduce the amount of memory usage. Commented Apr 6, 2018 at 10:50

1 Answer 1

1

This flag "--max-old-space-size=4096" is correct.
Simply try to add more:

--max-old-space-size=8192
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for your reply. I tried it but got the same error.
Try to remove some objects copying. const tempObject = parser.toJson(xmlObject);
I tried it again but still the same. I am reading the source file. I find this module uses another module called "node-expat" to parse xml file and the error comes from a function called "getError()" inside "node-expat". I guess it is a problem with this module but I have no idea of how to fix it yet. But thank you for your help again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.