2

I'd like to serialize some simple data model into xml, I've been using the standard java.org.w3c -related code (see below), the indentation is better than no "OutputKeys.INDENT", yet there is one little thing that remains - proper indentation for child elements.

I know that this has been asked before on stackoverflow , yet that configuration did not work for me, this is the code I'm using :

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();

        doc = addItemsToDocument(doc);
        // The addItemsToDocument method adds childElements to the document.

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", new Integer(4));
        // switching to setAttribute("indent-number", 4); doesn't help

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

         DOMSource source = new DOMSource(doc);
       StreamResult result = new StreamResult(outFile);
        // outFile is a regular File outFile = new File("some/path/foo.xml");

        transformer.transform(source, result);

The output produced is :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<stuffcontainer>
<stuff description="something" duration="240" title="abc">
<otherstuff />
</stuff>
</stuffcontainer>

Whereas I would want it (for more clarity) like :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<stuffcontainer>
  <stuff description="something" duration="240" title="abc">
    <otherstuff />
  </stuff>
</stuffcontainer>

I was just wondering if there is a way of doing this, make it properly indented for the child elements.

Thank you in advance !
Happy Easter coding :-) !

Community
  • 1
  • 1
Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
  • Possible duplicate of [Pretty-printing output from javax.xml.transform.Transformer with only standard java api (Indentation and Doctype positioning)](https://stackoverflow.com/questions/1264849/pretty-printing-output-from-javax-xml-transform-transformer-with-only-standard-j) – Thomas Weller Nov 16 '17 at 14:55

3 Answers3

15

If the Transformer implementation you're using is Xalan-J, then you should be able to use:

transformer.setOutputProperty(
   "{http://xml.apache.org/xslt}indent-amount", "5");

See also: http://xml.apache.org/xalan-j/usagepatterns.html

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • 1
    See over there, more complete answer : http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java – Benj Mar 28 '13 at 13:55
2
import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory

transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");
  • should not relay on com.sun.* packages in your code. for more information reffer the link .[link](http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html) – user2478236 Nov 17 '16 at 08:31
1
Document doc;

.....

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(new File("filename.xml")));
transformer.transform(new DOMSource(doc), new StreamResult(System.out));
iCrazybest
  • 2,935
  • 2
  • 24
  • 24
  • 1
    Op stated that this does not work for him... Check the link in the question. – Syon Sep 16 '14 at 11:45
  • 2
    In my case it is working fine ... it seems the issue was fixed later in the library https://bugs.openjdk.java.net/browse/JDK-5064280 fixed the issue – Rajesh Goel Mar 20 '17 at 16:34