aspose file tools*
The moose likes Java in General and the fly likes backslash n is being escaped against my will Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "backslash n is being escaped against my will" Watch "backslash n is being escaped against my will" New topic
Author

backslash n is being escaped against my will

Greg Diffor
Greenhorn

Joined: Mar 20, 2010
Posts: 4
I have an input string being read from an xml file. The string has a literal \n in it so that when it is written (in an email in this case) it will force a new line.

When I read the xml file (using apache's xerces), and assign the input to a String, the resulting String contains \\n (i.e. it is being escaped for me).

Here's the actual line of code:



So if my xml contains <content>blah blah blah\nblah blah blah</content>, the assignment to my String object is "blah blah blah\\nblah blah blah.

Because the \ is being escaped, the resulting email gets "un-escaped" and shows \n in it instead of recognizing the \n as an escape for "new line"

How do I keep an assignment into a String object from escaping the backslash or do I need to un-escape it manually using a REGEX?


G
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4640
    
    5

Greg Diffor wrote:I have an input string being read from an xml file. The string has a literal \n in it so that when it is written (in an email in this case) it will force a new line.

When I read the xml file (using apache's xerces), and assign the input to a String, the resulting String contains \\n (i.e. it is being escaped for me).

Here's the actual line of code:



So if my xml contains <content>blah blah blah\nblah blah blah</content>, the assignment to my String object is "blah blah blah\\nblah blah blah.

Because the \ is being escaped, the resulting email gets "un-escaped" and shows \n in it instead of recognizing the \n as an escape for "new line"

How do I keep an assignment into a String object from escaping the backslash or do I need to un-escape it manually using a REGEX?



A literal \n is not a newline. it's the two characters \ and n. "\n" in a String literal is a single character: a newline. So nothing's 'being escaped" here.


luck, db
There are no new questions, but there may be new answers.
Greg Diffor
Greenhorn

Joined: Mar 20, 2010
Posts: 4
That makes sense. When I read the xml and place it in a String object, then my "\" is escaped to make sure that it is handled as a single backslash.

I ended up using a unique character sequence of my own device to represent a new line. Then when the xml is parsed and the String is created, I do a



This results in a String object containing "stuff...\n...stuff"

I'm good now, but thanks for the clarification.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Greg Diffor wrote:I have an input string being read from an xml file. The string has a literal \n in it so that when it is written (in an email in this case) it will force a new line.


Okay. So now that you understand the underlying issues, it should be apparent to you that this idea was a design error. If you want a new line character in an XML text node you can just put a new line character there. You don't need to escape it like that. So your unescaping code is also unnecessary.
Greg Diffor
Greenhorn

Joined: Mar 20, 2010
Posts: 4
Don't know if that's true. The reason I had it in there in the first place is because I originally just had text in my xml formatted the way I'd like to see it in the email (with paragraphs separated by blank lines). When the email came out, the "new line" characters were treated like plain white space and everything was on one line.

So I put \n's in there to try to force new lines where I wanted them.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Greg Diffor wrote:Don't know if that's true. The reason I had it in there in the first place is because I originally just had text in my xml formatted the way I'd like to see it in the email (with paragraphs separated by blank lines). When the email came out, the "new line" characters were treated like plain white space and everything was on one line.

So I put \n's in there to try to force new lines where I wanted them.


Are you putting the data in a CDATA block?

If not, you should be, and then you should not have to do escaping inside the CDATA block, because that block is not parsed.


Steve
Greg Diffor
Greenhorn

Joined: Mar 20, 2010
Posts: 4
You're all full of great advice. I'm learning a lot here.

If I use a CDATA construct, will I also get all the other whitespace caused by indenting? I know it's a minor issue because I could just not indent it, but I may not be the only one maintaining this xml file.

shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Paul Clapham wrote:If you want a new line character in an XML text node you can just put a new line character there.

I didn't get it. I think thats what the problem is having \n in xml and that is getting modified to \\n.


SCJP 1.5(97%) My Blog
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Greg Diffor wrote:Don't know if that's true. The reason I had it in there in the first place is because I originally just had text in my xml formatted the way I'd like to see it in the email (with paragraphs separated by blank lines). When the email came out, the "new line" characters were treated like plain white space and everything was on one line.

So I put \n's in there to try to force new lines where I wanted them.


And did that make a difference?

I ask because it's true that XML text node can contain new-line characters, and they do actually represent new-line characters. And because e-mail clients will add or remove newline characters from messages before they display them sometimes for some reasons which I don't understand.

So you may have identified the wrong culprit for your problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Greg Diffor wrote:You're all full of great advice. I'm learning a lot here.

If I use a CDATA construct, will I also get all the other whitespace caused by indenting? I know it's a minor issue because I could just not indent it, but I may not be the only one maintaining this xml file.


Yes. And you'll get all the other whitespace even if you don't use a CDATA section. If there's whitespace in your document, and you don't have a schema or a DTD which describes it as unwanted, then it's part of the document and it's just as valid as any other text in the document.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4640
    
    5

shivendra tripathi wrote:
Paul Clapham wrote:If you want a new line character in an XML text node you can just put a new line character there.

I didn't get it. I think thats what the problem is having \n in xml and that is getting modified to \\n.

See the first response in this thread. If there's anything there that you don't understand, start your own thread to ask for clarification.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: backslash n is being escaped against my will