Home > Java > Write xml file with indentation

Write xml file with indentation

We are going to see how can transform an org.w3c.dom.Document object in an xml file.

public void writeXmlFile(Document doc, String filename)
			throws TransformerFactoryConfigurationError, TransformerException {
		Source source = new DOMSource(doc);
		// prepare output
		File xmlFile = new File( filename);
		Result result = new StreamResult(xmlFile);

		// write file
		Transformer transform = TransformerFactory.newInstance()
                //indent the xml content
		transform.setOutputProperty(OutputKeys.INDENT, "yes");
		transform.transform(source, result);

The indentation is done with this line by setting to yes the property “OutputKeys.INDENT”

//indent the xml content
transform.setOutputProperty(OutputKeys.INDENT, "yes");
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: