Recent

Author Topic: [SOLVED] XML - breaking new line in text content  (Read 30267 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1057
[SOLVED] XML - breaking new line in text content
« on: February 07, 2011, 02:42:17 pm »
Hi,

When text content have some special chars (like & < > ") then xml writter correctly replace this chars by &#amp; etc. But when my string have new line (#13#10 or #10) then it doesn't. It write:
Code: [Select]
<node>a
b
</node>
I send this xml generated by xmwritter to some server, but he can't recognize this new line. How to solve this?

Regards
« Last Edit: February 09, 2011, 05:29:27 pm by Dibo »

Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: XML - breaking new line in text content
« Reply #1 on: February 09, 2011, 11:21:44 am »
Replace all LineEndings in your string with '<br />' ?

Bart

sfeinst

  • Sr. Member
  • ****
  • Posts: 259
Re: XML - breaking new line in text content
« Reply #2 on: February 09, 2011, 01:36:46 pm »
I may be wrong, but the use of line endings is not guaranteed in XML as that represents formatting.  To keep your line endings in your text (not your overall file format), you should use the ![CDATA[your text here]] as in:
<tag1>
![CDATA[here is some text

with some newlines
]

If you mean that you format:
<tag1>
    <tag2>
       text
    </tag2>
</tag1>

and it comes out as:
<tag1><tag2>test</tag2></tag1>

then I don't think the writer will keep the formatting.

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: XML - breaking new line in text content
« Reply #3 on: February 09, 2011, 03:18:53 pm »
If you mean that you format:
<tag1>
    <tag2>
       text
    </tag2>
</tag1>

and it comes out as:
<tag1><tag2>test</tag2></tag1>
Those two fragments are definitely not the same. XML does not mean 'ignore all whitespace'.
In other words line endings are not formatting but content!!
That they are ignored by users (i.e. programs/applications) of an XML document is something completely different. (Or removed explicitely for example when defining XSLT transformations.)
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: XML - breaking new line in text content
« Reply #4 on: February 09, 2011, 05:29:03 pm »
Thanks. Server doesn't strip line endings (#13#10) when text is between ![CDATA[

sfeinst

  • Sr. Member
  • ****
  • Posts: 259
Re: XML - breaking new line in text content
« Reply #5 on: February 09, 2011, 10:45:46 pm »
Those two fragments are definitely not the same. XML does not mean 'ignore all whitespace'.
In other words line endings are not formatting but content!!
That they are ignored by users (i.e. programs/applications) of an XML document is something completely different. (Or removed explicitely for example when defining XSLT transformations.)


I don't disagree with what you are saying, just trying to figure out how XML is working.  I did a little more research and it looks like, within tags, end of lines are stored as line feeds.  Between tags, end of lines may be lost.  So if the OP was using Windows, what the OP might have been seeing is:

<tag1>
  <tag2>
Some text#13#10
  </tag2>
</tag1

converted as
<tag1><tag2>Some text#10</tag2></tag1>

Since CR/LF is not LF, whatever the OP was using to view the XML file may not have displayed the text correctly (as in using notepad).

But that is only if the OP was using Windows.  Obviously, this would not be the same issue in Linux.

Of course the issue may be with the writer not writning correctly unless CDATA is used.  I haven't looked at the code to see what it is doing.  I just know CDATA solved my problem when I ran into the same thing.

 

TinyPortal © 2005-2018