Skip to content
CodeKruPro

Extract from XML with XmlPath

49/62

Extract from XML with XmlPath

REST Assured provides XmlPath for XML extraction, similar to JsonPath for JSON:

XmlPath xmlPath = response.xmlPath();
String title = xmlPath.getString("library.book[2].title");  // third book's title

The path follows GPath syntax where elements are separated by dots and arrays use index notation.

Task: Extract the first user's name and email from the XML response.

Expected output:

Name: John Doe
Email: john@example.com

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

  1. Send a GET for /xml/users from given() and call xmlPath() on the response, keeping the result in a variable of the XmlPath type the starter already imports.
  2. Read the first user's name with getString(), using the path written in the starter comment. It starts at the users root and indexes the first user element.
  3. Read the email from the same element by changing only the last segment of the path.
  4. Print two lines: Name: followed by the name, then Email: followed by the email.

Common mistakes

  • Starting the path below the root

    A path such as user[0].name skips the users root. 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 a JsonPathException reporting that it failed to parse the JSON document. Use xmlPath() 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.

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.

Main.java
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"
    }
}