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.