html – Line Break in XML?

html – Line Break in XML?

@icktoofay was close with the CData

n

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

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

n

<xml>n  <text>some textnwithnthree lines</text>n</xml>n

n

and the contents of <text> will be

n

nsome textnwithnthree linesn

n

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

n


n

* 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.

n

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.

n


n

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.

n

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

n

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

n

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

n

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

html – Line Break in XML?

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

n

That special character defines the carriage-return character.

Leave a Reply

Your email address will not be published.