1
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite Name" parallel="none">
<listeners>
    <listener class-name="testng.MyListener"></listener>
</listeners>
  <test name="Test localhost">
    <parameter name="Browser" value="chrome"/> <!-- chrome or firefox or IE or Android Native-->
    <parameter name="url" value='' /> <!-- URL of the webpage you want to test -->
    <parameter name="CSV" value="data.csv"/> <!-- Location of the CSV file that you want to run tests with -->
    <parameter name="resultLocation" value="C:\Users\xvdf\Desktop\testmap"/> <!-- TestNG will create two folders in this location, screenshots and test-output-datestamp -->
    <parameter name="runType" value="baseline" /> <!--  actual or baseline . baseline = first run. actual is second run, to do a compare for example -->
    <classes>
      <class name="testng.runTest"></class>
    </classes>
  </test> <!-- Test CAN YOU SEE THIS?-->
</suite> <!-- Suite end of suite -->

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.

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();
       }
    }
}
4
  • Define attribute? what you are expecting? Commented Jan 14, 2015 at 16:35
  • Can you please show us (here or by a pastbin link) the output/errors of your program ? Commented Jan 14, 2015 at 16:36
  • Here is a tutorial, hope it'll help : journaldev.com/901/how-to-edit-xml-file-in-java-dom-parser Commented Jan 14, 2015 at 16:39
  • Why dont you use either getElementsByTagName to get the elements first (rather than chaining lots of getNextSibling methods) or use XPath to do it. Commented Jan 14, 2015 at 16:42

1 Answer 1

3

Seems like a homework of some kind so not gonna give you full source but below snippet takes care of the first parameter you want to change. Its cleaner than your attempted solution and more resistant to XML structure changes. You can use it to do the same thing for second parameter.

XPath xp = XPathFactory.newInstance().newXPath();
Element node = (Element) xp.evaluate("//test/parameter[@name='resultLocation']", 
                    doc, XPathConstants.NODE);
node.setAttribute("name", "new attribute value");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.