Reading XML can be done in two different with Selenium and Java
1.We can read XML file in Java from selenium framework with code.
Example XML File (testdata.xml):
<testdata>
<user>
<username>testuser</username>
<password>password123</password>
</user>
<url>https://example.com</url>
</testdata>
Java Code with DOM Parser:
package utilities;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.File;
import org.testng.annotations.Test;
public class ReadXML {
@Test
public void readTestDataXML() {
try {
File file = new File(“src/test/resources/testdata.xml”);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
String username = doc.getElementsByTagName(“username”).item(0).getTextContent();
String password = doc.getElementsByTagName(“password”).item(0).getTextContent();
String url = doc.getElementsByTagName(“url”).item(0).getTextContent();
System.out.println(“Username: ” + username);
System.out.println(“Password: ” + password);
System.out.println(“URL: ” + url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. We can also use JAXB framework to read the xml file with additional features:
JAXB (Java Architecture for XML Binding) maps XML directly to Java objects.
This is great for structured XML and larger automation frameworks.
Example XML File (user.xml)
<user>
<username>adminUser</username>
<password>adminPass</password>
<role>Admin</role>
</user>
Step 1: Create a Model Class
package model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = “user”)
public class User {
private String username;
private String password;
private String role;
@XmlElement
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@XmlElement
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return “User [username=” + username + “, password=” + password + “, role=” + role + “]”;
}
}
Step 2: Unmarshal XML to Java Object
package utilities;
import model.User;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import org.testng.annotations.Test;
public class ReadXMLWithJAXB {
@Test
public void readXMLFile() {
try {
File file = new File(“src/test/resources/user.xml”);
JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
User user = (User) jaxbUnmarshaller.unmarshal(file);
System.out.println(“Username: ” + user.getUsername());
System.out.println(“Password: ” + user.getPassword());
System.out.println(“Role: ” + user.getRole());
} catch (Exception e) {
e.printStackTrace();
}
}
}