How to write a new line character to xml file in java

Posted on 2006-07-07
Last Modified: 2008-01-09
i want to create an xml file from my class:

<equiz instruction="Hello \n World;" random="1"></equiz>

I want to preserve that newline character in the xml file and when i read it back in a JTextArea then it should be replaced by an Enter.

How can i write new line character to xml file in java? I am using the following method for writing the document to file:

// This method writes a DOM document to a file
    public static void writeXmlFile(Document doc, String filename) {
        try {
            // Prepare the DOM document for writing
            Source source = new DOMSource(doc);
            // Prepare the output file
            File file = new File(filename);
            Result result = new StreamResult(file);
            // Write the DOM document to the file
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
        } catch (TransformerException e) {

But when there is an enter in the text then it replaces it with &#xa; But i dont want it replaced with that.
Question by:imation3m
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 17057137
I think you can do this by writing the char code, which is 32, I think?
System.out.println(new Character(32)); // this might work
LVL 86

Accepted Solution

CEHJ earned 50 total points
ID: 17057219
You can't embed control characters in attributes. You need to replace the &#xa; you receive with \n

Author Comment

ID: 17057258
Actually i m creating an xml editor. The actual program which sombody wrote uses this xml as an input into it. So i need to be able to write \n in the attributes because that other program uses it. i cannot change the xml specification now.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

ID: 17057271
>>So i need to be able to write \n in the attributes because that other program uses it.

You can't
LVL 92

Expert Comment

ID: 17057315
use a cdata block
LVL 86

Expert Comment

ID: 17057359
A CDATA block won't help you. That's for element content, not attributes
LVL 86

Expert Comment

ID: 17079453

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 136
web project error add remove 1 85
jsp error 6 72
junit as external jar or library 7 59
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question