oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
921 stars 110 forks source link

Prolog not displaying #172

Closed HeavenlyHost closed 6 years ago

HeavenlyHost commented 6 years ago

Unable to get the prolog to display

<?xml version="1.0" encoding="utf-8"?>

This will not display... A code snippet

var writeToXML = function () { // Recreate the manifest file based on contents of the app directory

var xml = builder.create('Elements', { version: '1.0', encoding: 'UTF-8' });
xml.att({ 'xmlns': "http://schemas.microsoft.com/sharepoint/" });
var module = xml.ele('Module', { 'Name': 'app' });

fileList.forEach(function (file) {
    var modified = file.substring(2); // Removes first two characters as we don't need them
    var inverse = modified.split("/").join("\\");
    module.ele('File', { 'Path': inverse, 'Url': modified, 'ReplaceContent': "TRUE" });
});

xml.end({ pretty: true });

console.log(xml);

fs.writeFile("./app/test.xml", xml, function (err) {
    if (err) {
        return console.log(err);
    }
    console.log("The file was saved!");
});

};

oozcitak commented 6 years ago

You shouldn't directly use the xml object, but use the return value of end which is the xml object converted to a string. e.g.:

var xmlStr = xml.end({ pretty: true });

console.log(xmlStr);

fs.writeFile("./app/test.xml", xmlStr, function (err) {
    if (err) {
        return console.log(err);
    }
    console.log("The file was saved!");
});
HeavenlyHost commented 6 years ago

Fabulous that worked for me, KUDOS !

HeavenlyHost commented 6 years ago

Might be worth noting this on the readme :+1: