About this lesson
REST Assured XmlPath: pull values out of an XML response with GPath
Learn REST AssuredAdvancedXML Responses
How it works
Plenty of services still answer in XML: older enterprise systems, payment gateways, feeds. testkru serves one such document at /xml/users, labelled application/xml with a UTF-8 charset. The root element is <users>, and inside it sit five <user> elements, each holding id, name, email, age and city as child elements. Calling xmlPath() on the Response parses that body into an XmlPath object from io.restassured.path.xml, the XML twin of the JsonPath you have been using for JSON.
XmlPath expressions are Groovy GPath. A path begins at the root element name and walks down with dots, and square brackets pick one of several repeated siblings, counting from zero. So the root, then the repeated user element with an index, then the child you want. A negative index counts from the end, so [-1] picks the last user. Without an index, getString() on the name path glues all five names into one unbroken string, while getList() on the same path returns a proper list of five.
Two behaviours differ from JSON. A path that matches nothing does not give null here: dropping the root, overshooting the index or naming an element that does not exist all return an empty string, quietly. And XML is all text, so plain get() on the age element hands back the String "30". Ask for a type explicitly with getInt(), getDouble() or getBoolean() when you need one.
About the assignment
Fetch the XML user list, turn the body into an XmlPath, and print the first user's name and email, each after its label.
What you will do
- Send a GET for
/xml/usersfromgiven()and callxmlPath()on the response, keeping the result in a variable of theXmlPathtype the starter already imports. - Read the first user's name with
getString(), using the path written in the starter comment. It starts at theusersroot and indexes the firstuserelement. - Read the email from the same element by changing only the last segment of the path.
- Print two lines:
Name:followed by the name, thenEmail:followed by the email.
Common mistakes
Starting the path below the root
A path such as
user[0].nameskips theusersroot. XmlPath does not complain; it returns an empty string, so your label prints with nothing after it. Always lead with the root element.Parsing XML with jsonPath()
Habit from earlier lessons makes it tempting, but
jsonPath()on this body throws aJsonPathExceptionreporting that it failed to parse the JSON document. UsexmlPath()for XML.Forgetting the index
Without
[0]the name path covers every user.getString()then returns all five names run together with no separator, which looks like a data bug rather than a path bug.
In a real test suite
The same GPath works inside assertions, because REST Assured picks the XML parser from the response content type. then().body() with the root-first path and equalTo() checks a value without extracting it, and a path ending in size() counts repeated elements. GPath closures help too: find { it.name == 'Jane Smith' } on the user list locates an element by content instead of position.
When the XML comes from a file or a message queue rather than an HTTP call, construct new XmlPath(xmlString) directly and query it the same way.
Related lessons
- Get and Validate XML ResponseFetches the same XML user list and checks the raw body text before any parsing.
- XML Product ValidationMoves to the XML product feed, where price and stock flags need typed getters.
- Reading the Response BodyShows the JSON counterpart, reading a field with
jsonPath().getString().
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import io.restassured.path.xml.XmlPath;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /xml/users
// TODO: Get the XmlPath from the response
// TODO: Extract first user's name and email using GPath syntax
// Path: "users.user[0].name" and "users.user[0].email"
}
}