OtagoHarbour
OtagoHarbour

Reputation: 4183

Storing and retrieving new line '\n' in XML files

I am saving a text element in an XML file. Within the XML file, there are line breaks within the text node as follows

<text text="This is
a new line." fontsize="20">

However, when I load the XML file, using loadXMLDoc(), I use

textStr=textElem.getAttribute("text");
thisChar=textStr.charAt(i);
console.log('thisChar='+thisChar+'='+thisChar.charCodeAt(0));

to see if the '\n' characters have been retained and find that they have been replaced by spaces. (That is thisChar.charCodeAt(0) gives a value of 32 instead of a value of 10.)

What is the best way to save and retrieve new lines in text elements using XML?

Upvotes: 1

Views: 269

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

That's not a text node, it's an attribute node. The difference is crucial. In attributes, whitespace is automatically normalized by the XML parser - the details depend on how the attribute is described in the DTD, but by default newlines are replaced by spaces.

Upvotes: 2

Related Questions