title: Modify attributes in xml file
body: <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;suite name="Test Suite Name" parallel="none"&gt;
  &lt;listeners&gt;
      &lt;listener class-name="testng.MyListener"&gt;&lt;/listener&gt;
  &lt;/listeners&gt;
    &lt;test name="Test localhost"&gt;
      &lt;parameter name="Browser" value="chrome"/&gt; &lt;!-- chrome or firefox or IE or Android Native--&gt;
      &lt;parameter name="url" value='' /&gt; &lt;!-- URL of the webpage you want to test --&gt;
      &lt;parameter name="CSV" value="data.csv"/&gt; &lt;!-- Location of the CSV file that you want to run tests with --&gt;
      &lt;parameter name="resultLocation" value="C:\Users\xvdf\Desktop\testmap"/&gt; &lt;!-- TestNG will create two folders in this location, screenshots and test-output-datestamp --&gt;
      &lt;parameter name="runType" value="baseline" /&gt; &lt;!--  actual or baseline . baseline = first run. actual is second run, to do a compare for example --&gt;
      &lt;classes&gt;
        &lt;class name="testng.runTest"&gt;&lt;/class&gt;
      &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- Test CAN YOU SEE THIS?--&gt;
  &lt;/suite&gt; &lt;!-- Suite end of suite --&gt;
  </code></pre>
  
  <p>I am trying to edit this XML so I can change 2 parameters, resultLocation and runType. So far I have tried a few things with the DocumentBuilder, but I cant see to get it to find the attributes. This is the code I have tried so far. The first part works, I can get to the test element. </p>
  
  <pre><code>package testng;
  
  import java.io.File;
  import java.io.IOException;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerException;
  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.Element;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.SAXException;
  
  public class ModifyXMLFile {
  
      public static void main(String argv[]) {
  
         try {
          String filepath = "testng.xml";
          DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
          Document doc = null;
          try {
              doc = docBuilder.parse(filepath);
          } catch (SAXException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          // Get the root element
          Node suite = doc.getFirstChild();
          Node test = suite.getFirstChild().getNextSibling().getNextSibling().getNextSibling();
          System.out.println(test);
          NamedNodeMap testName = test.getAttributes();
          Node nsdf = testName.getNamedItem("name");
          if(nsdf.getNodeValue().equals("Test localhost")){
              System.out.println("Found Element!");
          }//This actually works.
  
          Node loc = test.getFirstChild().getNextSibling().getNextSibling();
          System.out.println(loc);
          NamedNodeMap sdf = loc.getAttributes();
          Node as = sdf.getNamedItem("Browser");
          if(as.getNodeValue().equals("chrome")){
              System.out.println("Browser found!");
          }
         } catch (ParserConfigurationException pce) {
          pce.printStackTrace();
         }
      }
  }
  </code></pre>