Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

How do I deal with new line in text when outputting to XML in Java

Thread view: 
John - 10 Jun 2007 11:45 GMT
Say I have a String in Java like

"To be or not to be, that is the question.
William Shakespear"

How can I convert it to a xml file that has two nodes, each of which
contains one line of that string? I mean, how do I program to determine the
new line?

<dramma>
        <text>To be or not to be, that is the question.</text>
        <text>William Shakespear</text>
</drama>
Lew - 10 Jun 2007 14:15 GMT
> Say I have a String in Java like
>
[quoted text clipped - 9 lines]
>          <text>William Shakespear</text>
> </drama>

If there is a new line in the input String, it's easy.  There are several
methods in the String class that can detect where the '\n' is; see the Javadocs.

If there is no new line in the input you're SOL.

Signature

Lew

Andrew Thompson - 10 Jun 2007 15:08 GMT
>> Say I have a String in Java like
>>
>> "To be or not to be, that is the question.  ...

It would be a question Willie, if you'd added a freakin'
question mark*.  (That's /always/ bugged me.)

>If there is a new line in the input String, it's easy.  There are several
>methods in the String class that can detect where the '\n' is; see the Javadocs.

What about \r, or (what was it?) \n\r.  Doing a
String.split() on both should do the trick, though.

>If there is no new line in the input you're SOL.

How about storing the original text as CDATA,
instead of splitting it into two fields?
Does that retain the formatting of the original
statement?

* And don't start with me about whether question marks
were used in Shakespeare's day.  The question mark
dates from around the 7th to 9th centuries, according
to Wikipedia.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

JT - 10 Jun 2007 15:47 GMT
>>> Say I have a String in Java like
>>>
[quoted text clipped - 20 lines]
> dates from around the 7th to 9th centuries, according
> to Wikipedia.

<sarcasm>
And wikipedia is always 100% accurate?
</sarcsasm>

just kidding.
Lew - 10 Jun 2007 16:40 GMT
>>>> Say I have a String in Java like
>>>>
>>>> "To be or not to be, that is the question.  ...
>>
>> It would be a question Willie, if you'd added a freakin' question
>> mark*.  (That's /always/ bugged me.)

You misconstrue the meaning of the word "question" in this context - it is
used in the sense of "issue" or "matter", as in "The Irish Question".
<http://en.wikipedia.org/wiki/A_Modest_Proposal>

I see no grammatical issue in omitting the question mark.

Signature

Lew

JT - 10 Jun 2007 17:10 GMT
> You misconstrue the meaning of the word "question" in this context - it
> is used in the sense of "issue" or "matter", as in "The Irish Question".
> <http://en.wikipedia.org/wiki/A_Modest_Proposal>
>
> I see no grammatical issue in omitting the question mark.

And you are replying to me because?
Twisted - 10 Jun 2007 21:18 GMT
> >>>> Say I have a String in Java like
>
[quoted text clipped - 8 lines]
>
> I see no grammatical issue in omitting the question mark.

Regardless. "To be or not to be?" might be a question, but "To be or
not to be, that is the question." is a statement. (A statement that
something else is a question. Confused yet?)

ObTheTopic: Eclipse rulz!
Mark Space - 10 Jun 2007 22:41 GMT
> Say I have a String in Java like
>
[quoted text clipped - 9 lines]
>          <text>William Shakespear</text>
> </drama>

I'm not the expert at all, but I think this will work.  Make a Reader
object from the String:

String willie = "To be or not to be, that is the question.\nWilliam
Shakespear"
StringReader willieReader = new StringReader( willie );

Then use that to make a BufferedReader, which has a readLine method.

BufferedReader bufferedWillieReader = new BufferedReader( willieReader );

String line = bufferedWillieReader.readLine();

I'm sure there's a way to do this just with String methods, but this
might be the most general, since the BufferReader will also let you read
from files.  I found this just by poking around the only Java API that
Sun maintains:

http://java.sun.com/j2se/1.3/docs/api/java/io/BufferedReader.html
Roedy Green - 15 Jun 2007 11:06 GMT
>"To be or not to be, that is the question.
>William Shakespear"
[quoted text clipped - 7 lines]
>         <text>William Shakespear</text>
></drama>

If there is a \n at the split, use indexOf('\n');
if there in a \r\n at the spit, us indexOf("\r\n");
If the . is the only indication, indexOf again.
You can also use Regex.split see http://mindprod.com/jgloss/regex.html

Convert your String to char[] and have a look at what you have to work
with.

see http://mindprod.com/jgloss/conversion.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.