Create a xml file from a string (solved)


Sorry for this dummy question, but I can;t get it to work… But how do I store a string to disk as xml? (I already have a xml file, and I want to dynamically insert the hostnames…)

var configFile =
    <?xml version="1.0"?>
    <SharedPersistenceConfiguration xmlns:xsi="" xmlns:xsd="">
    <DbHost>` + Meteor.settings.public.qlikSenseHost + `</DbHost>
    var file = path.join(, 'InstallationSoftware', 'spc.cfg');
    fs.outputFile(file, JSON.stringify(configFile, null, 2), 'utf-8');

–> stores a file, but this is not recognized as xml by the other program that needs to read it…

thanks a lot!

because you’re forcing it to be stringified as valid JSON, and since XML !== JSON the only way to make an XML string valid JSON is to wrap it in double quotes. example:

JSON.stringify(`<?xml version="1.0"?><a>asdfa</a></xml>`, null, 2)


"<?xml version=\"1.0\"?><a>asdfa</a></xml>"

notice the double quotes, which makes it invalid XML

sorry, my github did not sync well, I solved that error already (But thanks off course!), I tried fs.outputFile(file, configFile, 'utf-8');

which outputs (a not well formatted xml, but I don’t know if that matters…)

When I removed the extra space in the beginning of the xml string it now works!

1 Like