简体   繁体   中英

Getting a value from XML response in REST-Assured

I'm trying to get a value from an XML response using REST Assured , but I'm only getting empty values.

XML example:

<?xml version="1.0" ?>
<ncresponse
    orderID="50143601"
    STATUS="5"
    SCORING="1"
    SCO_CATEGORY="G">
</ncresponse>

My code:

RestAssured.useRelaxedHTTPSValidation();
Map<String, String> body = new HashMap<>();
body.put("ORDERID", orderId);
body.put("USERID", QUERY.getUser());
body.put("PSW", QUERY.getPass());
Response validation = given().proxy(host("myproxy.com").withPort(8080)
    ).params(body).when().get(QUERY.getUrl());

return from(validation.asString()).get("ncresponse.STATUS");

In this case, I'm trying to get the STATUS value ("5"), but all I'm getting is "" for any attribute.

Any help would be very appreciated.

You need to first convert the response to xml type and then get the value from that response.

First you need to import the following in your code:

import io.restassured.path.xml.XmlPath;

And then your code should be:

String stringResponse = validation.asString();
XmlPath xmlPath = new XmlPath(stringResponse);
String status = xmlPath.get("ncresponse.STATUS");

Now the String status contains the value which you want.

Well, i realized that I was making a mistake here. I was trying to get an attribute from the node "ncresponse", so using "@" symbol before the attribute is neccesary:

from(validation.asString()).get("ncresponse.@STATUS");

Im closing this, thanks for all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM