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

J

John

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

Lew

John said:
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.
 
A

Andrew Thompson

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.aspx/java-general/200706/1
 
J

JT

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


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


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.
 
T

Twisted

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!
 
M

Mark Space

John said:
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/api/java/io/BufferedReader.html
 
R

Roedy Green

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top