Hello everybody.

Here is the code that creates the XML file.

public void createFile(List<Integer> list) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("entries"); document.appendChild(root); for (Integer value : list) { Element subRoot = document.createElement("entry"); root.appendChild(subRoot); Element entry = document.createElement("field"); entry.setTextContent(String.valueOf(value)); subRoot.appendChild(entry); } StreamResult file = new StreamResult(new File("1.xml")); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(document); transformer.transform(source, file); } catch (ParserConfigurationException | TransformerException e) { e.printStackTrace(); } } 

It turns out a file with the following content:

 <entries> <entry> <field>1</field> </entry> <entry> <field>2</field> </entry> <entry> <field>3</field> </entry> ... </entries> 

How to bring it to this form?

 <entries> <entry> <field>1</field> </entry> <entry> <field>2</field> </entry> <entry> <field>3</field> </entry> ... </entries> 

How to make this indent? Thank.

    1 answer 1

    It is necessary to enable the indents of the INDENT and adjust their size (how much this indent will be the length, so to speak)

     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 
    • Thank you, Alexey! - Vyacheslav Chernyshov