title: Getting Error "The markup in the document following the root element must be well-formed." while fetching data from XML tags using Java
body: <p>I am getting this ERROR MSG in Eclipse :</p>
  
  <pre><code>[TestNG] Running:
    C:\Users\AppData\Local\Temp\testng-eclipse-69564752\testng-customsuite.xml
  
  [Fatal Error] TestExecutionDetails.xml:3:2: The markup in the document following the root element must be well-formed.
  
  FAILED: LoginTest
  org.xml.sax.SAXParseException; systemId: file:///C:/workspace/Pro1/bin/TestExecutionFiles/TestExecutionDetails.xml; lineNumber: 3; columnNumber: 2; The markup in the document following the root element must be well-formed.
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
      at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
      at Pro1.XMLData.getXMLtagValue(XMLData.java:29)
      at OR.Login.LoginTest(Login.java:17)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
      at org.testng.TestRunner.privateRun(TestRunner.java:774)
      at org.testng.TestRunner.run(TestRunner.java:624)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
      at org.testng.SuiteRunner.run(SuiteRunner.java:261)
      at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
      at org.testng.TestNG.run(TestNG.java:1048)
      at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
  
  ===============================================
      Default test
      Tests run: 1, Failures: 1, Skips: 0
  ===============================================
  
  ===============================================
  Default suite
  Total tests run: 1, Failures: 1, Skips: 0
  ===============================================    
  </code></pre>
  
  <p>Here is my XML :</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8" standalone="no" ?&gt;
      &lt;Browser&gt;Mozilla&lt;/Browser&gt;
      &lt;URL&gt;&lt;![CDATA[https://google.mail.com]]&gt;&lt;/URL&gt;
      &lt;Uid&gt;Testing&lt;/Uid&gt;
      &lt;Pwd&gt;Test&lt;/Pwd&gt;
  </code></pre>
  
  <p>Java Code to fetch data from XML Tags :</p>
  
  <pre><code>import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.xpath.XPath;
  import javax.xml.xpath.XPathConstants;
  import javax.xml.xpath.XPathFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class XMLData {
  
          public static String getXMLtagValue (String tagName) throws Exception{
  /*      FileInputStream instream = new FileInputStream("test.xml");
          BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
          StringBuilder str =  new StringBuilder();
          String line=null;
          while((line = reader.readLine())!=null){
              str.append(line);
          }*/
          String tagValue = null;
  
          String myXMLfile = "C:\\workspace\\Pro1\\bin\\TestExecutionFiles\\TestExecutionDetails.xml";
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          Document document = builder.parse(myXMLfile);
  
          XPath xpath = XPathFactory.newInstance().newXPath();
  
          String expression = "/"+tagName;
  
          NodeList nodeList = (NodeList)xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
  
          for (int i = 0; i &lt; nodeList.getLength(); i++) {
                 Node node = nodeList.item(i);
                 System.out.println("node name: "+node.getNodeName());
                 NodeList childNodes = node.getChildNodes();
                 for(int j=0;j&lt;childNodes.getLength();j++){
                     Node childNode = childNodes.item(j);
                     if(childNode.getNodeType() == Node.ELEMENT_NODE){
                         System.out.println(childNode.getNodeName()+"::"+childNode.getTextContent().trim());
                         System.out.println("--------attributes");
                         NamedNodeMap map = childNode.getAttributes();
                         for(int k=0; k&lt;map.getLength();k++){
                             Node attnode = map.item(k);
                             tagValue = attnode.getNodeName()+attnode.getTextContent().trim();
                         }
                         //System.out.println("--------");
                     }
  
                 }
                    //NamedNodeMap map = node.getAttributes();
          }
          return tagValue;
      }
  }
  </code></pre>
  
  <p>TestNG Program :</p>
  
  <pre><code>import java.util.concurrent.TimeUnit;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.support.PageFactory;
  import org.testng.annotations.Test;
  import PageFactory.*;
  
  public class Login {
  
      @Test
      public void LoginTest() throws Exception{
  
          String url = XMLData.getXMLtagValue("URL");
          String uid = XMLData.getXMLtagValue("Uid");
          String pwd = XMLData.getXMLtagValue("Pwd");
  
          WebDriver driver = Driver.callBrowser();
  
          driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
          driver.get(url);
          LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
          loginPage.username.sendKeys(uid);
          loginPage.pwd.sendKeys(pwd);
          loginPage.lbtn.click(); 
     }
  }
  </code></pre>
  
  <p>CData is also not working.
  And if there is any need for XSLT, please let me know how to work with it
  Please HELP !!!!!</p>