aspose file tools*
The moose likes Java in General and the fly likes Problem Retrieving String with new line. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem Retrieving String with new line." Watch "Problem Retrieving String with new line." New topic
Author

Problem Retrieving String with new line.

Gagan Popli
Greenhorn

Joined: Apr 17, 2011
Posts: 12
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

Joined: Jun 19, 2010
Posts: 825
    
    5

Did you try to do anything? We can discuss possible solutions (one of them using regular expressions, for example) but you need to ShowSomeEffort.


The quieter you are, the more you are able to hear.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

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?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Gagan Popli
Greenhorn

Joined: Apr 17, 2011
Posts: 12
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem Retrieving String with new line.
 
Similar Threads
mysql & incomplete insertions!
JMenu problem
'}' expected. Assignment 1.4
XML get number of node within duplicate node
How to split a string by the whitespace but ignoring '\n'?