xmldoc icon indicating copy to clipboard operation
xmldoc copied to clipboard

toString() adds extra rows that were never there; Also deletes first line in file.

Open atljoseph opened this issue 6 years ago • 4 comments
trafficstars

toString() adds extra rows that were never there; Also deletes first line in file.

I tried the options indicated in the readme file, but compressed literally compressed all into one line, preserveWhiteSpace added 2 lines between each node, and trimmed does not seem to affect anything.

Original format:

<?xml version='1.0' encoding='utf-8'?>
<parent>
    <node></node>
</parent>

Output Format:

<parent>

    <node></node>

</parent>

atljoseph avatar Apr 23 '19 05:04 atljoseph

Hi! toString() isn't designed to preserve the entire original document, it's just for debugging. But it should still work for most cases, you'll just need to add back the <?xml… declaration yourself.

I don't see those extra lines though, when I run your example:

var xmlString = `
<?xml version='1.0' encoding='utf-8'?>
<parent>
  <node></node>
</parent>
`.trim();
var parsed = new XmlDocument(xmlString);
var result = parsed.toString()
console.log(result);

It prints:

<parent>
  <node/>
</parent>

nfarina avatar Apr 23 '19 16:04 nfarina

I'm also seeing extra line breaks added - I'm on a Mac if that helps.

photomoose avatar Aug 24 '21 09:08 photomoose

Preserving whitespace works only when combined with compressed: toString({ compressed: true, preserveWhitespace: true }).

Without compressed it would add extra line breaks even if they are already there. The code from above prints the following for me (Ubuntu/WSL):

<parent>
  
  <node/>
  
</parent>

jakobharder avatar Oct 08 '21 18:10 jakobharder

I experience the issue described in this ticket.

josephdpurcell avatar Mar 04 '22 19:03 josephdpurcell