I'm writing a program that produces an xml file. The code works perfectly giving the expected result.But it gives me only a single line document. I want it to be in formatted way and correctly indented.Is there a way to do that? Here is the code i used
public class Main {
public static void main(String[] args) {
WriteXml(7, 9, 0);
}
public static void WriteXml(int WS1, int FTP1, int EMAIL1) {
try {
final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
final Document document = documentBuilder.newDocument();
final Element root = document.createElement("Counter");
document.appendChild(root);
final Element properties = document.createElement("properties");
root.appendChild(properties);
final Element webservice = document.createElement("age");
webservice.appendChild(document.createTextNode(Integer.toString(WS1)));
properties.appendChild(webservice);
final Element ftp = document.createElement("id");
ftp.appendChild(document.createTextNode(Integer.toString(FTP1)));
properties.appendChild(ftp);
final Element email = document.createElement("grade");
email.appendChild(document.createTextNode(Integer.toString(EMAIL1)));
properties.appendChild(email);
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final Transformer transformer = transformerFactory.newTransformer();
final DOMSource domSource = new DOMSource(document);
final StreamResult streamResult = new StreamResult(new File("src/counter.xml"));
transformer.transform(domSource, streamResult);
} catch (final Exception e) {
e.printStackTrace();
}
}
}
and this gives me the following xml document
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Counter><properties><age>7</age><id>9</id><grade>0</grade></properties></Counter>
But i want it to be more like this
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Counter>
<properties>
<age>7</age>
<id>9</id>
<grade>0</grade>
</properties>
</Counter>