• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

backslash n is being escaped against my will

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greg Diffor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greg Diffor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic