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:\[code\]<?xml version="1.0" encoding="UTF-8"?><configuration> <param0>true</param0> <param1>1.0</param1></configuration>\[/code\]And here are a couple of xPaths I want to insert/modify:\[code\]/configuration/param1/text() -> 4.0/configuration/param2/text() -> "asdf"/configuration/test/param3/text() -> true\[/code\]The XML file should look like this afterwards:\[code\]<?xml version="1.0" encoding="UTF-8"?><configuration> <param0>true</param0> <param1>4.0</param1> <param2>asdf</param2> <test> <param3>true</param3> </test></configuration>\[/code\]I tried this:\[code\]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\]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):\[code\]/configuration/param2/text()/configuration/test/param3/text()\[/code\]How can I change this code so that the node (and non existing parent nodes as well) are created?