Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 326695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:20:00+00:00 2026-05-12T09:20:00+00:00

I want to write some text that contains whitespace characters such as newline and

  • 0

I want to write some text that contains whitespace characters such as newline and tab into an xml file so I use

Element element = xmldoc.createElement("TestElement");
element.appendChild(xmldoc.createCDATASection(somestring));

but when I read this back in using

Node vs =  xmldoc.getElementsByTagName("TestElement").item(0);
String x = vs.getFirstChild().getNodeValue();

I get a string that has no newlines anymore.
When i look directly into the xml on disk, the newlines seem preserved. so the problem occurs when reading in the xml file.

How can I preserve the newlines?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:20:00+00:00Added an answer on May 12, 2026 at 9:20 am

    I don’t know how you parse and write your document, but here’s an enhanced code example based on yours:

    // creating the document in-memory                                                        
    Document xmldoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    
    Element element = xmldoc.createElement("TestElement");                                    
    xmldoc.appendChild(element);                                                              
    element.appendChild(xmldoc.createCDATASection("first line\nsecond line\n"));              
    
    // serializing the xml to a string                                                        
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();             
    
    DOMImplementationLS impl =                                                                
        (DOMImplementationLS)registry.getDOMImplementation("LS");                             
    
    LSSerializer writer = impl.createLSSerializer();                                          
    String str = writer.writeToString(xmldoc);                                                
    
    // printing the xml for verification of whitespace in cdata                               
    System.out.println("--- XML ---");                                                        
    System.out.println(str);                                                                  
    
    // de-serializing the xml from the string                                                 
    final Charset charset = Charset.forName("utf-16");                                        
    final ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes(charset));       
    Document xmldoc2 = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
    
    Node vs =  xmldoc2.getElementsByTagName("TestElement").item(0);                           
    final Node child = vs.getFirstChild();                                                    
    String x = child.getNodeValue();                                                          
    
    // print the value, yay!                                                                  
    System.out.println("--- Node Text ---");                                                  
    System.out.println(x);                                                                    
    

    The serialization using LSSerializer is the W3C way to do it (see here). The output is as expected, with line separators:

    --- XML --- 
    <?xml version="1.0" encoding="UTF-16"?>
    <TestElement><![CDATA[first line
    second line ]]></TestElement>
    --- Node Text --- 
    first line
    second line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write some text directly to a file using Flex 3 /
I want to write some bogus text in a file (helloworld text in a
I want to write some configuration in web.config file, that will make my ASP.Net
i have xml file which contains some details i want to pick some value
so my problem is simple. I want to Encrypt some text and write it
I want to write some tel numbers to excel file, some of them start
I have some text which occasionally contains very long strings of characters, which is
I have an XML that contains several flags, some of them are unsigned 32-bit
I have a long text which contains paths to some files. What I want
i want to write some kind of TagCloud/Selection in ASP.NET 4. So that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.