Read Data from XML - Notes By ShariqSP
How to Read Data from XML in Android Mobile Application Testing (Using Eclipse)
XML (eXtensible Markup Language) is commonly used for structured data storage in mobile application testing. It is ideal for scenarios like managing configuration data, test inputs, or expected results. This guide provides steps to read XML files in Android mobile testing using Eclipse.
Steps to Read Data from XML
-
Set Up the Environment:
- Ensure you have installed the Android Development Tools (ADT) plugin in Eclipse.
- Set up your Android project in Eclipse.
-
Place the XML File:
- Store the XML file in the
assets
folder of your Android project. - Right-click the
assets
folder in Eclipse and selectNew
>File
to add the XML file.
- Store the XML file in the
-
Use XML Parsers:
- Use an XML parser like
DOM Parser
,SAX Parser
, orXmlPullParser
. - Android natively supports
XmlPullParser
, which is lightweight and efficient.
- Use an XML parser like
-
Write Code to Parse the XML File:
- Access the XML file using
InputStream
. - Parse the XML using the selected parser and extract data.
- Access the XML file using
Sample Code in Eclipse
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.InputStream;
public class XmlReaderExample {
public void readXmlDataFromAssets(Context context) {
try {
// Access XML file from assets folder
InputStream inputStream = context.getAssets().open("testdata.xml");
// Create XmlPullParserFactory and parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, null);
// Variables to hold parsed data
String tag = null;
String username = null;
String password = null;
String expectedResult = null;
// Parse XML
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
tag = parser.getName();
break;
case XmlPullParser.TEXT:
if ("username".equals(tag)) {
username = parser.getText();
} else if ("password".equals(tag)) {
password = parser.getText();
} else if ("expectedResult".equals(tag)) {
expectedResult = parser.getText();
}
break;
case XmlPullParser.END_TAG:
if ("testcase".equals(parser.getName())) {
// Print parsed data
System.out.println("Username: " + username);
System.out.println("Password: " + password);
System.out.println("Expected Result: " + expectedResult);
}
break;
}
eventType = parser.next();
}
// Close resources
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Example XML File
testuser1
password123
Success
testuser2
wrongpassword
Error
Real-Time Scenarios
-
Login Testing:
- Store multiple sets of credentials in an XML file.
- Read the data using an XML parser and simulate login attempts.
- Validate the app's responses against the expected results.
-
Configuration Testing:
- Use XML to store app configuration details like API endpoints, feature flags, or environment variables.
- Verify the app's behavior by loading these configurations dynamically during testing.
-
Dynamic Content Testing:
- Use XML to define content for dynamic UI elements like forms or menus.
- Automate testing to validate the app renders and processes this content correctly.
Advantages of Using XML
- XML is a standard format for structured data, ensuring compatibility with various tools.
- It supports nested and hierarchical data, ideal for representing complex test cases.
- Readily parsable with Android's native
XmlPullParser
.