title: Dynamic TestNG XML Creation. Getting wrong XML. Where I am wrong
body: <p>Dynamic TestNG XML Creation. Getting wrong XML. Where I am wrong.</p>
  
  <p>I want my testNG xml to be print as shown below.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="App Automation Testing"&gt;
    &lt;parameter name="BrowserName" value="chrome"/&gt;
    &lt;test name="MyTest1"&gt;
      &lt;classes&gt;
        &lt;class name="etaf.tests.LaunchApp"/&gt;
        &lt;class name="etaf.tests.LoginTests"/&gt;
      &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- MyTest1 --&gt;
    &lt;test name="MyTest2"&gt;
      &lt;classes&gt;
        &lt;class name="etaf.tests.LaunchApp"/&gt;
      &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- MyTest2 --&gt;
  &lt;/suite&gt; &lt;!-- App Automation Testing --&gt;
  </code></pre>
  
  <p>But with the below Java Code; this code is having a array similar to what shown below</p>
  
  <pre><code>|testclass|testname|
  |class1   |TC_LOGIN|
  |class2   |TC_LOGIN|
  |class1   |TC_WORK |
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>public void sample(String[][] dbArr, Map&lt;String,String&gt; parameters ) {
  
          //Create an instance on TestNG
          TestNG myTestNG = new TestNG();
  
          //Create an instance of XML Suite and assign a name for it.
          XmlSuite suite = new XmlSuite();
          suite.setName("App Automation Testing");
          suite.setParameters(parameters);
  
          String dummyName = "";
          List&lt;XmlClass&gt; classes1 = null;
          List&lt;XmlTest&gt; tests = new ArrayList&lt;XmlTest&gt;();
          XmlTest test1 = null;
  
  
          for(int i=0;i&lt;dbArr.length;i++) {
              if(!dummyName.equalsIgnoreCase(dbArr[i][1])) {
                  test1 = new XmlTest(suite);
                  test1.setName(dbArr[i][1]);
                  classes1 = new ArrayList&lt;XmlClass&gt; ();
                  classes1.add(new XmlClass(dbArr[i][0]));
                  dummyName = dbArr[i][1];
  
              } else if(dummyName.equalsIgnoreCase(dbArr[i][1])) {
                  classes1.add(new XmlClass(dbArr[i][0]));
                  dummyName = dbArr[i][1];
              }
              test1.setXmlClasses(classes1);
              tests.add(test1);
          }
  
          //add the list of tests to your Suite.
          suite.setTests(tests);
  
          //Add the suite to the list of suites.
          List&lt;XmlSuite&gt; suites = new ArrayList&lt;XmlSuite&gt;();
          suites.add(suite);
  
          System.out.println(suite.toXml());
  
          //Set the list of Suites to the testNG object you created earlier.
          myTestNG.setXmlSuites(suites);
  
          //invoke run() - this will run your class.
          myTestNG.run();
      }
  </code></pre>
  
  <p><strong>it is printing like this. Where I am wrong.? Please help.</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="App Automation Testing"&gt;
    &lt;parameter name="BrowserName" value="chrome"/&gt;
    &lt;test name="TC_LOGIN"&gt;
      &lt;classes&gt;
        &lt;class name="etaf.tests.LaunchApp"/&gt;
        &lt;class name="etaf.tests.LoginTests"/&gt;
      &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- TC_LOGIN --&gt;
    &lt;test name="TC_LOGIN"&gt;
      &lt;classes&gt;
        &lt;class name="etaf.tests.LaunchApp"/&gt;
        &lt;class name="etaf.tests.LoginTests"/&gt;
      &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- TC_LOGIN --&gt;
    &lt;test name="TC_WORK"&gt;
      &lt;classes&gt;
        &lt;class name="etaf.tests.LaunchApp"/&gt;
      &lt;/classes&gt;
    &lt;/test&gt; &lt;!-- TC_WORK --&gt;
  &lt;/suite&gt; &lt;!-- App Automation Testing --&gt;
  </code></pre>