This is possible by using the library's importDocument function and, depending on your needs, using other libraries as well. Key is to recognize limitations in xmlbuilder-js and other libraries, then strategize accordingly. For example, after calling xmlbuild-js.create(), you cannot traverse through the document from root to get at existing child elements, you can only append to the end. Knowing this, you can plan how to create your structure in JSON, or import xml from a file to JSON, manipulate that, then use xmlbuilder-js.create() when the JSON is ready. Or you might use importDocument to insert fragments as needed. Another strategy would be to re-design your xml docs so they are are fragments, or just get fragments from them, and you can build the root in xml builder.
Here is an example of appending documents from xmlbuilder-js documentation
var peopleNode = xmlbuilder.create('roster').ele('people');
for (var i = 1; i <= 3; i++) { // Create an XML fragment person =
xmlbuilder.create('person').att('id', i); // Import the root node of
the fragment after // the people node of the main XML document
peopleNode.importDocument(person);
}
Instead of using xmlbuilder-js to create the person, for example, you could import a file with an xml fragment for person, using xml2js-parser, as in this example from their docs:
const fs = require('fs');
const Parser = require('xml2js-parser');
var parser = new Parser({trim: true});
fs.readFile(__dirname + '/foo.xml', (err, xml) => {
parser.parseString(xml, (err, result) => {
console.dir(result);
});
});
That is the approach I took, I happened to see your question when trying to do the same thing.