File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "New Line Character not recognized" Watch "New Line Character not recognized" New topic

New Line Character not recognized

Shweta Gulati

Joined: Apr 26, 2001
Posts: 15
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.
Ilja Preuss

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
I agree. Here's the link:
subject: New Line Character not recognized
It's not a secret anymore!