Create a xml file from a string (solved)

hi,

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="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 ...
    <DbHost>` + Meteor.settings.public.qlikSenseHost + `</DbHost>
...
    </SharedPersistenceConfiguration>
    `;
    //SAVE Silent install CONFIG TO THE EXPORT FOLDER
    var file = path.join(Meteor.settings.broker.automationBaseFolder, '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)

yields

"<?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