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

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.
Who is Participating?
CEHJConnect With a Mentor Commented:
You can't embed control characters in attributes. You need to replace the &#xa; you receive with \n
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
imation3mAuthor Commented:
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.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

You can't
use a cdata block
A CDATA block won't help you. That's for element content, not attributes
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.