2

I'm serializing an XML document to String using JavaScript, but I can't get rid of the line breaks.

Simple XML Example:

<list>
    <item label='one'/>
    <item label='two'/>
</list>

When I use:

var xmlSerializer = new XMLSerializer();
xmlString = xmlSerializer.serializeToString(xml);

...the line breaks and indentations are preserved (visible when printing xmlString) but I want the whole XML to look like this:

<list><item label='one'/><item label='two'/></list>

I tried this:

xmlString.replace(/(\r\n|\n|\r)/gm,"");

...but I don't find it a reliable and clean solution. It should only affect the XML structure, not the content within the XML elements.

Any idea?

2
  • Your code seems to work for me, just remember that replace will return a new string, so try xmlString = xmlString.replace(/(\r\n|\n|\r)/gm,""); Commented Mar 8, 2018 at 15:17
  • It might be, that it just looks as if your XML contains these characters. Many tools to print or view an XML prettyfy it automatically... Commented Mar 8, 2018 at 15:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.