title: How do I append a node to an existing XML file in java
body: <pre><code>public static void addALLToXML(Collection&lt;Server&gt; svr) throws IOException,
        ParserConfigurationException, TransformerException
  {
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
          .newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory
          .newDocumentBuilder();
      Document document = documentBuilder.newDocument();
  
      // Root Element
      Element rootElement = document.createElement("Servers");
      document.appendChild(rootElement);
  
      for (Server i : svr)
      {
          // server elements
          Element server = document.createElement("server");
          rootElement.appendChild(server);
  
          Element name = document.createElement("name");
          name.appendChild(document.createTextNode(i.getName()));
          server.appendChild(name);
  
          Element port = document.createElement("port");
          port.appendChild(document.createTextNode(Integer.toString(i.getPort())));
          server.appendChild(port);
      }
  
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();
      DOMSource source = new DOMSource(document);
  
      StreamResult result = new StreamResult("/home/user/server.xml");
      transformer.transform(source, result);
  }
  </code></pre>
  
  <p>This is the function I need help with:</p>
  
  <pre><code>public static void addNodeToXML(String nameIn, String portIn)
        throws ParserConfigurationException, SAXException, IOException
  {
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
              .newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory
              .newDocumentBuilder();
  
      /* parse existing file to DOM */
      Document document = documentBuilder
              .parse(new File("/home/user/server.xml"));
  
      // Root Element
      Element rootElement = document.createElement("Servers");
      document.appendChild(rootElement);
  
      // server elements
      Element server = document.createElement("server");
      rootElement.appendChild(server);
  
      Element name = document.createElement("name");
      name.appendChild(document.createTextNode(nameIn));
      server.appendChild(name);
  
      Element port = document.createElement("port");
      port.appendChild(document.createTextNode(portIn));
      server.appendChild(port);
  }
  </code></pre>
  
  <p>Original:</p>
  
  <pre><code>&lt;Servers&gt;
   &lt;server&gt;
    &lt;name&gt;something&lt;/name&gt;
    &lt;port&gt;port&lt;/port&gt;
   &lt;/server&gt;
   &lt;/Servers&gt;
  </code></pre>
  
  <p>Wanted:</p>
  
  <pre><code>&lt;Servers&gt; 
    &lt;server&gt;
     &lt;name&gt;something&lt;/name&gt;
     &lt;port&gt;port&lt;/port&gt;
    &lt;/server&gt;
    &lt;server&gt;
     &lt;name&gt;something&lt;/name&gt;
     &lt;port&gt;port&lt;/port&gt;
    &lt;/server&gt;
  &lt;Servers&gt;
  </code></pre>