Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I deal with new line in text when outputting to XML in Java

Reply
Thread Tools

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

 
 
John
Guest
Posts: n/a
 
      06-10-2007
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>


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      06-10-2007
John wrote:
> 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>


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.

--
Lew
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      06-10-2007
Lew wrote:
>> 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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200706/1

 
Reply With Quote
 
JT
Guest
Posts: n/a
 
      06-10-2007
Andrew Thompson wrote:
> Lew wrote:
>>> 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.
>


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

just kidding.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-10-2007
JT wrote:
> Andrew Thompson wrote:
>> Lew wrote:
>>>> 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.

--
Lew
 
Reply With Quote
 
JT
Guest
Posts: n/a
 
      06-10-2007
Lew wrote:

> 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?
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      06-10-2007
On Jun 10, 11:40 am, Lew <(E-Mail Removed)> wrote:
> JT wrote:
> > Andrew Thompson wrote:
> >> Lew wrote:
> >>>> 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.


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!

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-10-2007
John wrote:
> 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>
>
>


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/ap...redReader.html
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-15-2007
On Sun, 10 Jun 2007 06:45:47 -0400, "John" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>"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>
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Problem Outputting Command Line Arguments from argv Matt C Programming 20 07-26-2007 02:52 AM
AMD64 or Semperon, deal or no deal? Tad Confused Computer Information 7 04-13-2006 05:43 PM
deal or no deal rbt Python 7 12-28-2005 08:57 PM
Outputting in-line image from BLOB in ASP.NET Jonathan ASP .Net 4 12-15-2004 10:34 PM



Advertisments