How to parse data from XML file in Selenium with Java?

There are various ways to use parameterization in Selenium. It depends on the tester which technique he/she is going to use to inject test data in the test case. Well, we have discussed various ways here at our beloved blog- Inviul.  😊 I am going to give links to those tutorials as well here, in case if you missed those awesome articles. Let’s take our learning journey further and discuss another way of parameterization through an XML file, which is today’s agenda as well.

Parameterization Recap:

Pre-Requisites

There are no such pre-requisites required before working on test data parsing from the XML file. We don’t require any external JAR file or any plugins. We only need IDE and Java should be well configured in your system with all the correct environment variables.

XML File

What is XML file?

XML stands for Extensible Markup Language. It is a self-descriptive markup language like HTML which is basically used to store and transport data from one channel to another. XML is a W3C recommendation.

Points to Ponder

  • XML fully takes care of the data
  • It is used to transport data
  • There are no pre-defined tags in XML like HTML

What is XML Parser?

An XML parser is a technique to access and manipulate XML files by loading its XML DOM.

What is XML DOM?

An XML DOM (Document Object Model) is a standard way to access and manipulate the message written inside XML documents. The entire DOM looks like the tree structure.

The info from XML DOM is retrieved by using JavaScript methods.

What is XML Node?

Every element written inside an XML file is a node.

What is Node List?

A list of nodes which consist of child nodes as well. Every element of the node is retrieved by using getElementsByTagName() method.

Sample XML file

Here is the sample XML file whose data we are going to read through Java for our Selenium project.

<?xml version = "1.0"?>
<bank>
   <account acn = "001">
      <firstname>avinash</firstname>
      <lastname>kumar</lastname>
      <balance>50000</balance>
   </account>
   
   <account acn = "002">
      <firstname>john</firstname>
      <lastname>cena</lastname>
      <balance>500</balance>
   </account>

   
   <account acn = "003">
      <firstname>michel</firstname>
      <lastname>dsouza</lastname>
      <balance>1000</balance>
   </account>
</bank>

In the above sample XML file, the bank is the head node and account is the child node. We will further get values of each node written inside the account node.

Test data parsing from XML file in Selenium WebDriver

Let’s understand the code which is used for parsing the XML file.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbf.newDocumentBuilder();

These two lines of code create the instance of the DOM architecture of the XML file.

Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();

These codes parse the XML file and normalize it for test data retrieval.

Further, we use JavaScript methods to get the texts So here is the entire program to read the test data from an XML file.

package Test;

import java.io.File;
import java.util.Iterator;
import java.util.ListIterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class XMLFileReader {

	public static void main(String[] args) {
		try{
			String filePath = "C:\\Users\\blogg\\Desktop\\input.xml";
			File file = new File(filePath);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbf.newDocumentBuilder();
			Document doc = dBuilder.parse(file);
			doc.getDocumentElement().normalize();
			System.out.println(doc.getDocumentElement().getNodeName());
			NodeList nodeList = doc.getElementsByTagName("account");
			int tLength = nodeList.getLength();
			
		
		for(int i=0; i<tLength; i++){
				Node node = nodeList.item(i);
				
				if(node.getNodeType()==Node.ELEMENT_NODE){
					Element element = (Element)node;
					System.out.println("Account No: "+element.getAttribute("acn"));
					System.out.println("First Name: "+element.getElementsByTagName("firstname").item(0).getTextContent());
					System.out.println("Last Name: "+element.getElementsByTagName("lastname").item(0).getTextContent());
					System.out.println("Balance: "+element.getElementsByTagName("balance").item(0).getTextContent());
				}
				
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	
	}

}

Here is the console output.

XML File Console output

This was all about reading test data from the XML file. If I missed anything then you can give your suggestions in the comment below.

Join Inviul fb group

One Response

Leave a Reply