title: Write to end of xml file using java
body: <p>I have an existing XML file below that I would like to write to using Java. I'm trying to add another pattern element with file and version-id within.  What I am doing is pulling the value of file and version-id from the last pattern element and then increasing the version numbers of those values.  I'm already successfully able to do that and set the new file and version values. I just now need to add them to the xml under a new pattern element.</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;jnlp-versions&gt;
        &lt;resource&gt;
          &lt;pattern&gt;
              &lt;file&gt;somejar_2_100_1_client.jar&lt;/file&gt;
              &lt;version-id&gt;2.100.1&lt;/version-id&gt;
          &lt;/pattern&gt;
          &lt;pattern&gt;
              &lt;file&gt;somejar_2_100_2_client.jar&lt;/file&gt;
              &lt;version-id&gt;2.100.2&lt;/version-id&gt;
          &lt;/pattern&gt;
          &lt;name&gt;jarclient.jar&lt;/name&gt;
         &lt;/resource&gt;
      &lt;/jnlp-versions&gt;
  </code></pre>
  
  <p>*****EDIT:</p>
  
  <p>I used xPath to get the last resource Node. Once I got that I then appended a new pattern Node to it with my new file and version-id tags within.</p>
  
  <pre><code>Node resourceNode = (Node)xPath.evaluate("/jnlp-versions/resource[last()]",doc.getDocumentElement(), XPathConstants.NODE);
  
      Element fileNode = doc.createElement("file");
      Element versionNode = doc.createElement("version-id");
  
      fileNode.setTextContent(newFileName);
      versionNode.setTextContent(newVersion);
  
      Element patternNode = doc.createElement("pattern");
      patternNode.appendChild(fileNode);
      patternNode.appendChild(versionNode);
      resourceNode.appendChild(patternNode);
  </code></pre>