Line Break in XML? [duplicate] Line Break in XML? [duplicate] xml xml

Line Break in XML? [duplicate]


@icktoofay was close with the CData

<myxml>    <record>        <![CDATA[        Line 1 <br />        Line 2 <br />        Line 3 <br />        ]]>    </record></myxml>


In XML a line break is a normal character. You can do this:

<xml>  <text>some textwiththree lines</text></xml>

and the contents of <text> will be

some textwiththree lines

If this does not work for you, you are doing something wrong. Special "workarounds" like encoding the line break are unnecessary. Stuff like \n won't work, on the other hand, because XML has no escape sequences*.


* Note that &#xA; is the character entity that represents a line break in serialized XML. "XML has no escape sequences" means the situation when you interact with a DOM document, setting node values through the DOM API.

This is where neither &#xA; nor things like \n will work, but an actual newline character will. How this character ends up in the serialized document (i.e. "file") is up to the API and should not concern you.


Since you seem to wonder where your line breaks go in HTML: Take a look into your source code, there they are. HTML ignores line breaks in source code. Use <br> tags to force line breaks on screen.

Here is a JavaScript function that inserts <br> into a multi-line string:

function nl2br(s) { return s.split(/\r?\n/).join("<br>"); }

Alternatively you can force line breaks at new line characters with CSS:

div.lines {    white-space: pre-line;}


At the end of your lines, simply add the following special character: &#xD;

That special character defines the carriage-return character.