0

When parsing an xmlstring, the \r\n is lost. How to solve that?

//Example
var string = "<x y=\"line1\r\nline2\"></x>",
    xml = $.parseXML(string),
    y = xml.documentElement.getAttribute("y");
//Now y is missing the \r\n.
Niels Steenbeek
  • 4,692
  • 2
  • 41
  • 50
  • Use escape `\\` for each `\r` `\n` – Java_User Feb 21 '14 at 11:36
  • @Java_User thanks. That works. I'm getting the value from an textarea, so expected the escaping is done by them. A setAttribute on xml works without escaping. Lessons learned and thanks again. – Niels Steenbeek Feb 21 '14 at 11:45
  • Also see this thread: http://stackoverflow.com/questions/2004386/how-to-save-newlines-in-xml-attribute – Tomalak Feb 21 '14 at 12:58

1 Answers1

2

A line break in an attribute value is meaningless.

<foo bar="line1
line2" />

is really equivalent to:

<foo bar="line1 line2" />

Encode your attribute value like this instead:

<foo bar="line1&#xA;line2" />
Tomalak
  • 332,285
  • 67
  • 532
  • 628