Programmatically creating a DOM tree

Main.java:

import org.w3c.dom.*;
 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
  
public class Main
{
   public static void main(String []args) {
      Document doc;
  
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         doc = db.newDocument();
  
         Element customers = doc.createElement("customers");
         Element customer = doc.createElement("customer");
         Element name = doc.createElement("name");
         Element address = doc.createElement("address");
         Element addressline = doc.createElement("addressline");
         Element zip = doc.createElement("zip");
         Element location = doc.createElement("location");
         Element country = doc.createElement("country");
 
         name.appendChild(doc.createTextNode("Joris Van den Bogaert"));
         addressline.appendChild(doc.createTextNode("Handelskaai 3"));
         zip.appendChild(doc.createTextNode("1000"));
         location.appendChild(doc.createTextNode("Brussels"));
         country.appendChild(doc.createTextNode("BELGIUM"));
          
         customer.setAttribute("id", "cust1");
         customer.appendChild(name);
         address.appendChild(addressline);
         address.appendChild(zip);
         address.appendChild(location);
         address.appendChild(country);
         customer.appendChild(address);
 
         customers.appendChild(customer);
 
         doc.appendChild(customers);
  
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer transformer = tf.newTransformer();
         transformer.transform(new DOMSource(doc), new StreamResult(System.out));
 
      }
      catch(Exception e) {
         e.printStackTrace();
      }
 
   }  
}

outputs (cust.xml):

<? xml version="1.0" encoding="UTF-8"?>
<customers><customer id="cust1"><name>Joris Van den Bogaert</name>
<address><addressline>Handelskaai 3</addressline><zip>1000</zip>
<location>Brussels</location><country>BELGIUM</country></address>
</customer></customers>