title: Add new element to xml file with Java DOM
body: <p>I have been working with a simple cd catalog XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;
  &lt;CATALOG&gt;
  &lt;CD&gt;
  &lt;TITLE&gt;Empire Burlesque&lt;/TITLE&gt;
  &lt;ARTIST&gt;Bob Dylan&lt;/ARTIST&gt;
  &lt;COUNTRY&gt;USA&lt;/COUNTRY&gt;
  &lt;COMPANY&gt;Columbia&lt;/COMPANY&gt;
  &lt;PRICE&gt;10.90&lt;/PRICE&gt;
  &lt;YEAR&gt;1985&lt;/YEAR&gt;
  &lt;/CD&gt;
  &lt;CD&gt;
  &lt;TITLE&gt;Hide your heart&lt;/TITLE&gt;
  &lt;ARTIST&gt;Bonnie Tyler&lt;/ARTIST&gt;
  &lt;COUNTRY&gt;UK&lt;/COUNTRY&gt;
  &lt;COMPANY&gt;CBS Records&lt;/COMPANY&gt;
  &lt;PRICE&gt;9.90&lt;/PRICE&gt;
  &lt;YEAR&gt;1988&lt;/YEAR&gt;
  &lt;/CD&gt;
  &lt;/CATALOG&gt;
  </code></pre>
  
  <p>With this method I want to create a new <code>CD</code> element:</p>
  
  <pre><code>private static void addCd(File xml) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, SAXException, IOException {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document document = db.parse(xml);
  
          Element cd = document.createElement("CD");
  
          document.appendChild(cd);
          for (int i = 1; i &lt;= 3; i++) {
              Element title = document.createElement("TITLE");
              Element artist = document.createElement("ARTIST");
              Element country = document.createElement("COUNTRY");
              Element company = document.createElement("COMPANY");
              Element price = document.createElement("PRICE");
              Element year = document.createElement("YEAR");
  
  
              title.appendChild(document.createTextNode("mike "));
              artist.appendChild(document.createTextNode("oconnor "));
              country.appendChild(document.createTextNode("ie "));
              company.appendChild(document.createTextNode("dell "));
              price.appendChild(document.createTextNode("14 "));
              year.appendChild(document.createTextNode("2014 "));
  
              cd.appendChild(title);
              cd.appendChild(artist);
              cd.appendChild(country);
              cd.appendChild(company);
              cd.appendChild(price);
              cd.appendChild(year);
          }
  
          TransformerFactory factory = TransformerFactory.newInstance();
          Transformer transformer = factory.newTransformer();
          DOMSource domSource = new DOMSource(document);
          StreamResult streamResult = new StreamResult(new File("cdCatalog.xml"));
          transformer.transform(domSource, streamResult);
  
          DOMSource source = new DOMSource(document);
  
      }
  </code></pre>
  
  <p>I can create a new <code>CD</code> element, but the new element appears outside my root (<code>&lt;CATALOG&gt;</code>) element:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;&lt;CATALOG&gt;
  &lt;CD&gt;
  &lt;TITLE&gt;Empire Burlesque&lt;/TITLE&gt;
  &lt;ARTIST&gt;Bob Dylan&lt;/ARTIST&gt;
  &lt;COUNTRY&gt;USA&lt;/COUNTRY&gt;
  &lt;COMPANY&gt;Columbia&lt;/COMPANY&gt;
  &lt;PRICE&gt;10.90&lt;/PRICE&gt;
  &lt;YEAR&gt;1985&lt;/YEAR&gt;
  &lt;/CD&gt;
  &lt;CD&gt;
  &lt;TITLE&gt;Hide your heart&lt;/TITLE&gt;
  &lt;ARTIST&gt;Bonnie Tyler&lt;/ARTIST&gt;
  &lt;COUNTRY&gt;UK&lt;/COUNTRY&gt;
  &lt;COMPANY&gt;CBS Records&lt;/COMPANY&gt;
  &lt;PRICE&gt;9.90&lt;/PRICE&gt;
  &lt;YEAR&gt;1988&lt;/YEAR&gt;
  &lt;/CD&gt;
  &lt;/CATALOG&gt;&lt;CD&gt;&lt;TITLE&gt;mike &lt;/TITLE&gt;&lt;ARTIST&gt;oconnor &lt;/ARTIST&gt;&lt;COUNTRY&gt;ie &lt;/COUNTRY&gt;&lt;COMPANY&gt;dell &lt;/COMPANY&gt;&lt;PRICE&gt;14 &lt;/PRICE&gt;&lt;YEAR&gt;2014 &lt;/YEAR&gt;&lt;TITLE&gt;mike &lt;/TITLE&gt;&lt;ARTIST&gt;oconnor &lt;/ARTIST&gt;&lt;COUNTRY&gt;ie &lt;/COUNTRY&gt;&lt;COMPANY&gt;dell &lt;/COMPANY&gt;&lt;PRICE&gt;14 &lt;/PRICE&gt;&lt;YEAR&gt;2014 &lt;/YEAR&gt;&lt;TITLE&gt;mike &lt;/TITLE&gt;&lt;ARTIST&gt;oconnor &lt;/ARTIST&gt;&lt;COUNTRY&gt;ie &lt;/COUNTRY&gt;&lt;COMPANY&gt;dell &lt;/COMPANY&gt;&lt;PRICE&gt;14 &lt;/PRICE&gt;&lt;YEAR&gt;2014 &lt;/YEAR&gt;&lt;/CD&gt;
  </code></pre>
  
  <p>I think I appended the wrong element, but have no idea how to append to <code>CATALOG</code>.</p>