title: Modify XML file with xPath
body: <p>I want to modify an existing XML file using xPath. If the node doesn't exist, it should be created (along with it's parents if neccessary). An example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration&gt;
    &lt;param0&gt;true&lt;/param0&gt;
    &lt;param1&gt;1.0&lt;/param1&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>And here are a couple of xPaths I want to insert/modify:</p>
  
  <pre><code>/configuration/param1/text()         -&gt; 4.0
  /configuration/param2/text()         -&gt; "asdf"
  /configuration/test/param3/text()    -&gt; true
  </code></pre>
  
  <p>The XML file should look like this afterwards:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration&gt;
    &lt;param0&gt;true&lt;/param0&gt;
    &lt;param1&gt;4.0&lt;/param1&gt;
    &lt;param2&gt;asdf&lt;/param2&gt;
    &lt;test&gt;
      &lt;param3&gt;true&lt;/param3&gt;
    &lt;/test&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I tried this:</p>
  
  <pre><code>import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.xpath.XPath;
  import javax.xml.xpath.XPathConstants;
  import javax.xml.xpath.XPathFactory;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamResult;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  try {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    Document doc = domFactory.newDocumentBuilder().parse(file.getAbsolutePath());
    XPath xpath = XPathFactory.newInstance().newXPath();
  
    String xPathStr = "/configuration/param1/text()";
    Node node = ((NodeList) xpath.compile(xPathStr).evaluate(doc, XPathConstants.NODESET)).item(0);
    System.out.printf("node value: %s\n", node.getNodeValue());
    node.setNodeValue("4.0");
  
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(doc), new StreamResult(file));
  } catch (Exception e) {
    e.printStackTrace();
  }
  </code></pre>
  
  <p>The node is changed in the file after running this code. Exactly what I wanted. But if I use one of the below paths, <code>node</code> is null (and therefore a <code>NullPointerException</code> is thrown):</p>
  
  <pre><code>/configuration/param2/text()
  /configuration/test/param3/text()
  </code></pre>
  
  <p>How can I change this code so that the node (and non existing parent nodes as well) are created?</p>
  
  <p><strong>EDIT</strong>: Ok, to clarify: I have a set of parameters that I want to save to XML. During development, this set can change (some parameters get added, some get moved, some get removed). So I basically want to have a function to write the current set of parameters to an already existing file. It should override the parameters that already exist in the file, add new parameters and leave old parameters in there.</p>
  
  <p>The same for reading, I could just have the xPath or some other coordinates and get the value from the XML. If it doesn't exist, it returns the empty string.</p>
  
  <p>I don't have any constraints on how to implement it, xPath, DOM, SAX, XSLT... It should just be easy to use once the functionality is written (like BeniBela's solution).</p>
  
  <p>So if I have the following parameters to set:</p>
  
  <pre><code>/configuration/param1/text()         -&gt; 4.0
  /configuration/param2/text()         -&gt; "asdf"
  /configuration/test/param3/text()    -&gt; true
  </code></pre>
  
  <p>the result should be the starting XML + those parameters. If they already exist at that xPath, they get replaced, otherwise they get inserted at that point.</p>