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

Discussion in 'Java' started by John, Jun 10, 2007.

  1. John

    John Guest

    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>
     
    John, Jun 10, 2007
    #1
    1. Advertising

  2. John

    Lew Guest

    Re: How do I deal with new line in text when outputting to XML inJava

    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
     
    Lew, Jun 10, 2007
    #2
    1. Advertising

  3. 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.aspx/java-general/200706/1
     
    Andrew Thompson, Jun 10, 2007
    #3
  4. John

    JT Guest

    Re: How do I deal with new line in text when outputting to XML inJava

    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.
     
    JT, Jun 10, 2007
    #4
  5. John

    Lew Guest

    Re: How do I deal with new line in text when outputting to XML inJava

    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
     
    Lew, Jun 10, 2007
    #5
  6. John

    JT Guest

    Re: How do I deal with new line in text when outputting to XML inJava

    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?
     
    JT, Jun 10, 2007
    #6
  7. John

    Twisted Guest

    On Jun 10, 11:40 am, Lew <> 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!
     
    Twisted, Jun 10, 2007
    #7
  8. John

    Mark Space Guest

    Re: How do I deal with new line in text when outputting to XML inJava

    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/api/java/io/BufferedReader.html
     
    Mark Space, Jun 10, 2007
    #8
  9. John

    Roedy Green Guest

    On Sun, 10 Jun 2007 06:45:47 -0400, "John" <> 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
     
    Roedy Green, Jun 15, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan
    Replies:
    4
    Views:
    1,109
    bruce barker
    Dec 15, 2004
  2. academic
    Replies:
    10
    Views:
    660
    academic
    Feb 15, 2006
  3. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    566
    Duncan Smith
    Dec 28, 2005
  4. Matt
    Replies:
    20
    Views:
    744
    Keith Thompson
    Jul 26, 2007
  5. Water Cooler v2
    Replies:
    0
    Views:
    421
    Water Cooler v2
    Nov 2, 2007
Loading...

Share This Page