Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atljoseph opened this issue Apr 23, 2019 · 4 comments

Comments

@atljoseph
Copy link

atljoseph commented Apr 23, 2019

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>
@nfarina
Copy link
Owner

nfarina commented Apr 23, 2019

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>

@photomoose
Copy link

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

@jakobharder
Copy link

jakobharder commented Oct 8, 2021

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>

@josephdpurcell
Copy link

I experience the issue described in this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants