const docString = '<svg/>'; // simple example using an svg
const docType = 'image/svg+xml';
const parser = new DOMParser();
const svgDocElement = parser.parseFromString(docString, docType); // parse
const xmlSerializer = new XMLSerializer();
const serialized = xmlSerializer.serializeToString(svgDocElement); // stringify
console.log(docString === serialized) // true