3

I am reading a string from an xml file stored on the sdcard that has some carriage returns stored with the "\n" escape sequence. Such as

<string mystring="Line1\nLine2">

But when I display the text in the emulator, the \n is showing up instead of making a newline. I am not doing anything unusual with the text--just reading it with a SAXParser and then adding it to a textview. Is there a setting I need to check to make sure newlines are rendered correctly? Do I need to store the carriage returns differently in the xml file?

I have also tried \r\n and that doesn't work either. When I debug, I can see that an extra \ is placed in front of the existing \ I see that in my SAX startElement method, the line

String s = atts.getValue("mystring");

assigns "Line1\nLine2" to s, so the problem is with the SAXParser.

MrGibbage
  • 2,644
  • 5
  • 39
  • 50

4 Answers4

4
&#xA;

works very well in the middle of a quoted textview. I'd like to add that in Eclipses' graphic viewer it puts in an extra space (like it is double spaced). When it is compiled however and you run it on the emulator or a device, you only have the carriage return. So, trust MrGibbage and you'll save yourself some time.

3

\n is actually a line feed character, while carriage return is \r. Try using both together, e.g. \r\n.

More info about newline.

For handling the whitespace with SAX take a look at ignorableWhitespace(..) method.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
2

Well, I figured out an answer. I had to store the newlines in my xml file ampersand-hash-x-A-semicolon

&#xA;
MrGibbage
  • 2,644
  • 5
  • 39
  • 50
1

Use a &#10; where you need the carraige return.

PeteH
  • 1,870
  • 25
  • 23