title: How do I use xpath in Java to find a node value or attribute in an xml and replace it with another value?
body: <p>Here is my Code so far:</p>
  
  <pre><code>// locate the node(s)
  XPath xpath = XPathFactory.newInstance().newXPath();
  NodeList nodes = (NodeList)xpath.evaluate("//root[text()='input[1]']", doc, XPathConstants.NODESET);
  
  // make the change
  for (int idx = 0; idx &lt; nodes.getLength(); idx++) {
      nodes.item(idx).setTextContent(input[3]);
  }
  
  // save the result
  Transformer xformer = TransformerFactory.newInstance().newTransformer();
  xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
  </code></pre>
  
  <hr>
  
  <p>Input[1] is what I am looking for in the XML, and input [3] is what it is being replaced with. </p>
  
  <p>Here is the XML:</p>
  
  <pre><code>&lt;root&gt; 
      &lt;accounts&gt; 
          &lt;account name="Bill Gates"&gt;
              &lt;position&gt;CEO&lt;/position&gt; 
              &lt;phoneNumber&gt;123-485-1854&lt;/phoneNumber&gt;
              &lt;notes&gt;Runs the company&lt;/notes&gt; 
          &lt;/account&gt; 
          &lt;account name="Service Account"/&gt; 
          &lt;account name="Burt Mackland"&gt; 
              &lt;position&gt;CFO&lt;/position&gt; 
              &lt;phoneNumber&gt;345-415-4813&lt;/phoneNumber&gt; 
              &lt;notes&gt;Great CFO&lt;/notes&gt; &lt;/account&gt; 
          &lt;account name="Joe Smith"&gt; 
              &lt;position&gt;Accountant&lt;/position&gt;
              &lt;reportsTo&gt;Burt Mackland&lt;/reportsTo&gt;
              &lt;phoneNumber&gt;135-118-7815&lt;/phoneNumber&gt; 
              &lt;notes&gt;Must have ID checked at all Gates&lt;/notes&gt;
          &lt;/account&gt; 
      &lt;/accounts&gt; 
      &lt;departments&gt; 
          &lt;deparment name="Finance"&gt; 
              &lt;employeeCount&gt;2&lt;/employeeCount&gt; 
          &lt;/deparment&gt; 
          &lt;department name="Human Resources"&gt; 
              &lt;employeeCount&gt;0&lt;/employeeCount&gt; 
          &lt;/department&gt; 
          &lt;department name="Executive"&gt; 
              &lt;employeeCount&gt;2&lt;/employeeCount&gt;
          &lt;/department&gt; 
      &lt;/departments&gt; 
  &lt;/root&gt;
  </code></pre>
  
  <p>The user may not know what is in the XML. So i cannot hardcode the Xpath in the code.
  Please any would be greatly appreciated!</p>