Solved

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

Posted on 2006-07-07
7
3,070 Views
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.
0
Comment
Question by:imation3m
7 Comments
 
LVL 8

Expert Comment

by:colr__
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
0
 
LVL 86

Accepted Solution

by:
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
0
 

Author Comment

by:imation3m
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

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

You can't
0
 
LVL 92

Expert Comment

by:objects
ID: 17057315
use a cdata block
0
 
LVL 86

Expert Comment

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

Expert Comment

by:CEHJ
ID: 17079453
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now