0

I am parsing from an xml document into an array. In my xml doc there are some line breaks notated in this format:


 

My Eclipse console shows them correctly with a line break if i sysout the arraylist. But is it somehow possible to show what is really saved in that variable?

Example:

sysout(myarray[1]);

current output:

xyz

xyz

wanted output:(or something like that)

xyz\n\nxyz
Lola Parola
  • 93
  • 2
  • 8
  • 2
    the current output _is_ what is really saved in the variable. if you want to escape certain characters for display, that is a different story, entirely unrelated to xml. – jtahlborn Jan 24 '13 at 13:32
  • possible duplicate of [Print escaped representation of a String](http://stackoverflow.com/questions/13701672/print-escaped-representation-of-a-string) – jlordo Jan 24 '13 at 13:32
  • The answer you are looking for has already been posted http://stackoverflow.com/questions/7888004/how-do-i-print-escape-characters-in-java – Steven Ackley Jan 24 '13 at 13:33
  • Look at `StringEscapeUtils` from [Apache Commons Lang](http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html#escapeJava%28java.lang.String%29), it has methods to (un)escape Java, XML and many more. – jlordo Jan 24 '13 at 13:33
  • 1
    @stevenackley: But the accepted answer is not nice at all. Rater use `StringEscapeUtils` – jlordo Jan 24 '13 at 13:35
  • I would have said the value without the escape sequences is the real value. – Peter Lawrey Jan 24 '13 at 13:35

2 Answers2

2

You can use the method escapeJava at org.apache.commons.lang.StringEscapeUtils from the Apache Commons Lang library. Take a look at the javadoc. Download the library here.

Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
0

You can use

string.replace("\n", "\\n");
TechSpellBound
  • 2,505
  • 6
  • 25
  • 36