How to understand this Java code that produces DOM output?
07:46 03 Nov 2018

With an XML file as an input, I've parsed it and modified it with basic functions like deleting a node, adding one etc. I wanted to output the final document in the form of a DOM tree. I've found this code but struggle to really understand what is doing each line (even when reading the doc for each functions).

Could you, please, illustrate with an example how it works?

  private static void toString(Document newDoc) throws Exception{
        DOMSource domSource = new DOMSource(newDoc);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
        System.out.println(sw.toString());  
      }

Output

java xml dom