It's not a secret anymore!*
The moose likes Java in General and the fly likes New Line Character not recognized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "New Line Character not recognized" Watch "New Line Character not recognized" New topic
Author

New Line Character not recognized

Shweta Gulati
Greenhorn

Joined: Apr 26, 2001
Posts: 15
Hi,
I am trying to read the XML file using the DOM Parser and one of the lines in that XML file has value with new line characters i.e \n. Now when i try to write the data retrieved i.e. the line with the new line character in the text area then the new line character is appearing as it is. i.e. instead of printing the text appearing after \n in a new line it is printing \n followed by the text in the same line.
For eg.
If the text read from XML file is
"Please write this to a \n new line."
The result expected in the text area would be
Please write this to a
new line.
But the result that I am getting is
Please write this to a \n new line.
The same thing if I hard code in a String variable and setText of the textarea with this hard coded value it is giving expected result i.e.
Please write this to a
new line.
Thanks a lot.
Shweta
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
\n is an escape sequence in java source code and is translated at compile time to the corresponding character as defined by the synctactic rules of java.
XML has its own syntactic rules, which are totally independent of the ones of java. After all, you want to have XML files be handled identically regardless of the programming language used to parse it.
So you have to use the XML rules to insert the newline character.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
saager mhatre
Ranch Hand

Joined: Dec 20, 2000
Posts: 61
You might be using '\\n' to actually put the '\' in the output. Use only '\n' to get the desired output
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: New Line Character not recognized
 
Similar Threads
textarea not refreshing from bookmarklet
XML question
write newline(\n) back to file
Regarding files
Problem writing a string into a file