Mobile Automation

Read Data from XML in Java and Integrate with Appium for Mobile Automation

Read Data from XML in Java and Integrate with Appium for Mobile Automation

By Bhau Automation Lab • XML + Appium Complete Mobile Automation Guide

🎯 What You Will Learn in This Tutorial

  • How to read data from XML file in Java
  • How to parse XML using DocumentBuilder
  • How to use XML data in Appium tests
  • Integrating XML with mobile automation framework
  • Best practices for XML handling in automation
  • Real-world mobile automation examples

📌 What is XML in Automation Testing?

XML (Extensible Markup Language) is widely used in automation testing to store test data, configuration details, and object information. In mobile automation, XML helps to manage data efficiently and keep test scripts clean.

Using XML with Appium allows you to separate test data from test logic, making your framework more maintainable and scalable.

⭐ Benefits of Using XML in Appium Automation

  • Easy data management
  • Reusable test data
  • Clean and maintainable code
  • Better framework structure
  • Supports large datasets
  • Ideal for data-driven testing

📂 Sample XML File Structure


    
        admin
        admin123
    
    
        test
        test123
    

⚙ Step 1: Read XML File in Java

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

import java.io.File;

public class ReadXMLData {

    public static void main(String[] args) {
        try {
            File file = new File("testdata.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);

            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("user");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String username = element.getElementsByTagName("username").item(0).getTextContent();
                    String password = element.getElementsByTagName("password").item(0).getTextContent();

                    System.out.println("Username: " + username);
                    System.out.println("Password: " + password);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

📱 Step 2: Use XML Data in Appium Test

driver.findElement(By.id("com.demo:id/username")).sendKeys(username);
driver.findElement(By.id("com.demo:id/password")).sendKeys(password);
driver.findElement(By.id("com.demo:id/login")).click();

This allows you to dynamically use XML data in your mobile automation tests.

🏢 Real-World Use Cases

  • Login automation using XML data
  • Multiple user testing
  • Configuration management
  • Environment handling
  • Large scale mobile automation frameworks

👨‍💻 Who Should Learn This?

  • QA Engineers & Testers
  • Mobile Automation Engineers
  • Appium Beginners
  • Java Automation Developers
  • Automation Framework Designers

🎥 Watch Full Video Tutorial

👉 Read XML in Java and Integrate with Appium – Full Practical Demo

✅ Key Takeaways

  • XML is perfect for storing test data
  • Java makes XML parsing simple and powerful
  • Appium can easily use XML data
  • Improves framework quality and scalability
  • Essential skill for mobile automation testers
Pro Tip: Always validate your XML file structure before using it in automation to avoid runtime errors.

🚀 Created with ❤️ by Bhau Automation Lab

Back to All Articles