• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem Retrieving String with new line.

 
Gagan Popli
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am stuck in a very weird issue. I have a String object with XML String. e.g.


Now, what i require, is to get the data within tag <CCC></CCC> as is how you see in the above code snippet.
i.e. like this

Address : xxxx
Name : yyyy
DOB : uuuu

done

Please help..

Thanks.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to do anything? We can discuss possible solutions (one of them using regular expressions, for example) but you need to ShowSomeEffort.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gagan,

Welcome to CodeRanch!

As Kemal suggested, please, ShowSomeEffort and TellTheDetails - as in, did you face any issues when you tried? What have you tried yet and what issues did you face?
 
Gagan Popli
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes,

What i did was, to use the Pattern, Matcher and did find() on the matcher object.
That worked for <AAA> tag or <BBB> tag , as there is only one string value in them.
But since <CCC> has string with new lines, so it is not able to retrieve it. And the compiler complains
with error message "String literal is not properly closed by a double-quote"




But i wish to retrieve the <CCC>tag data as it looks above.

That's something i tried.

Thanks n Regards
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will get you only characters in the same line with <CCC> and </CCC>.
What you need to pick is: any character, colon and whitespace (space, tab, new line, ...) between those two. So:

I would recommend you do it this way anytime you work with regular expressions. First say it in English (or whatever your native language is) what you want to match, and then jump to creating regex. Here it's pretty straightforward: any word character ( \w ), colon ( : ) and whitespace ( \s ). You just need to add asterisk ( * ) to denote that you're looking for those zero or more times (Pattern API). This will give you the desired result, also with opening and closing "CCC" tag that you can remove easily in your next step.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original pattern is correct, but the Pattern object needs to be compiled with at least the Pattern.DOTALL flag. That makes the ".*" part include the line breaks, which isn't the case by default.

As for the "String literal is not properly closed by a double-quote" error message, you can't create String literals with line breaks in them like that. You need to replace each line break with \n (or \r\n):
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic