In the last chapter, we have learned how to parse the JSON response and get values from the JSON response. Sometimes we need to extract values from the XML files, Having XML response is an old-style but still some applications use (which are not migrated to JSON).
We are using the XML endpoint at : https://chercher.tech/sample/api/books.xml
We have to use a bath to get specific or arrays of values from the XML. I have listed a few below which may help you to understand things a little better.
Lets try to get a specific element from the XML response. We have to use the extract() method to get the value from the response, the response could be XML or it could be JSON.
path() method gets a value from the response body using the JsonPath or XmlPath syntax. REST Assured will automatically determine whether to use JsonPath or XmlPath based on the content-type of the response.
import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
public class XMLValuesExtraction {
@Test
void sampleTest() {
String book = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book.title");
System.out.println(book);
}
}
The Output
The Nightingale
we can get the values of the sibling/child elements based on a specific element. For example, consider, you want to retrieve the name of the author whose books are in a specific category, then we can use this kind of XML path.
@Test
void sampleTest() {
String book = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book.findAll { [email protected] == 'cooking' }.year");
System.out.println(book);
}
The output
2015
You can use the deep search when you are not sure of the part elements but you know only the values, then in such cases, you can use the deep search. A deep search will find all the matching elements irrespective of their location in the XML.
This also helps in reducing the length of the XML path that we were writing.
@Test
void sampleTest() {
String book = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("**.findAll { [email protected] == 'cooking' }.year");
System.out.println(book);
}
The output:
2015
When there is more than one matching element then you may see the below error.
In such cases, please use the Type as
@Test
void sampleTest() {
NodeChildrenImpl book = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book.findAll { [email protected] == 'cooking' }.price");
System.out.println(book);
}
Similar to extracting a single value we can extract multiple values as well. To extract multiple values we have to use another class called NodeChildrenImpl.
@Test
void sampleTest() {
NodeChildrenImpl books = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book.title");
System.out.println(books);
}
The output ( returned as a single string)
The NightingaleHarry Potter
Using NodeChildrenImpl class, we can perform other operations on the examine extracted. I have listed a few below.
list we can get the output in list formatimport org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
import io.restassured.internal.path.xml.NodeChildrenImpl;
public class XMLValuesExtraction {
@Test
void sampleTest() {
NodeChildrenImpl books = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book.title");
System.out.println("just single string: "+ books);
System.out.println("spcific index : "+ books.get(0));
System.out.println("is empty : "+ books.isEmpty());
System.out.println("size : "+ books.size());
System.out.println("list : "+ books.list());
}
}
The output
In the above program, we have seen how to use
When we try to fetch a subset of XML, we might need to extract the values from the subset of XML. In such cases, we can use
Example to fetch the subset of XML:
@Test
void sampleTest() {
NodeChildrenImpl books = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book");
System.out.println("list : "+ books);
System.out.println("list : "+ books.get(0));
}
Don't be scared that in the above output we have not received the subset XML; we have received subset XML but the
So we will be performing operations on the first match (Please read the program as well for better understanding).
@Test
void sampleTest() {
NodeChildrenImpl books = RestAssured.given().when()
.get("https://chercher.tech/sample/api/books.xml")
.then().extract().path("bookstore.book");
System.out.println("name : "+ books.get(0).name());
System.out.println("attributes : "+ books.get(0).attributes());
System.out.println("getAttribute : "+ books.get(0).getAttribute("category"));
System.out.println("get : "+ books.get(0).get("year"));
System.out.println("children : "+ books.get(0).children());
System.out.println("children : "+ books.get(0).children().get("price"));
System.out.println("getNode : "+ books.get(0).getNode("price").children().get(0));
}
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.