What I'm trying is to format a string variable, which represents an XML file, with line breaks.
// This represents an XML file, but it's not formatted
let xmlStr = "<?xml version="1.0"?><?meta name="test"?><Foo> <Bar> <FooBar/> </Bar></Foo>";
Now, I'm trying to come up with a function to format the string
xmlStr = formatXmlStr(xmlStr);
console.log(xmlStr);
The console.log above is supposed to print the following.
<?xml version="1.0"?>
<?meta name="test"?>
<Foo>
<Bar>
<FooBar/>
</Bar>
</Foo>
I found that Javascript offers XMLSerializer, which returns an XML string based on a DOM object. However...
const docObj = (new DOMParser()).parseFromString(xmlStr, 'text/xml');
const xmlString = (new XMLSerializer()).serializeToString(docObj);
console.log(xmlString);
The resulting xmlString isn't formatted (i.e. It has no line breaks).
Is it possible to add line breaks to an XML string with Javascript?
UPDATE
Formatting has to be done without changing tabs (or blank spaces) in the XML string. This means, for example, solutions posted on this link won't be used because the resulting strings will have different lengths of tabs.