简体   繁体   English

从 REST-Assured 中的 XML 响应中获取值

[英]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.我正在尝试使用REST Assured从 XML 响应中获取值,但我只获取空值。

XML example: XML 示例:

<?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.在这种情况下,我试图获取STATUS值(“5”),但我得到的只是任何属性的""

Any help would be very appreciated.任何帮助将不胜感激。

You need to first convert the response to xml type and then get the value from that response.您需要首先将响应转换为 xml 类型,然后从该响应中获取值。

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.现在字符串status包含您想要的值。

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:我试图从节点“ncresponse”获取一个属性,因此在需要该属性之前使用“@”符号:

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

Im closing this, thanks for all!我关闭了,谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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