Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DOM is introducing newlines

843834Jul 31 2004 — edited Aug 3 2004
Hi, I have a very simple program that parses an XML file into a DOM document, and then dumps it to System.out. here it is:
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class DumpDOM
{
    public static void main(String[] args) throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory
            .newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(args[0]));

        // Prepare the DOM document for writing
        Source source = new DOMSource(document);

        // Prepare the output file
        Result result = new StreamResult(System.out);

        // Write the DOM document to the file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer xformer = transformerFactory.newTransformer();
        xformer.transform(source, result);
    }
}
Now, I run it with the following file named test.xml that contains:
<html>
<body>

</body>
</html>
I run it like this:
java DumpDOM test.xml

I would expect the same xml to be dumped, instead, the output looks like this:
<html>

<body>


</body>

</html>
Can anyone tell me why it introduces newlines that were not there before? and most importantly how can I prevent it?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 31 2004
Added on Jul 31 2004
1 comment
152 views