【问题标题】:How to extract value from SOAP XML response with multiple namespaces using Rest-assured?如何使用 Rest-assured 从具有多个命名空间的 SOAP XML 响应中提取值?
【发布时间】:2020-11-23 09:49:24
【问题描述】:

我使用 Rest Assured 进行的调用返回了以下 SOAP 响应:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header>
      <epns:context xmlns:epns="http://mywebservice/v6">
         <epns:userId>SYSTEM</epns:userId>
         <epns:systemId>WEBSERVICE</epns:systemId>
         <epns:realmId />
      </epns:context>
   </env:Header>
   <S:Body>
      <ns0:liststatusResponse xmlns:ns0="http://mywebservice/v6/workflow" xmlns:asst="http://mywebservice/v6" xmlns:status="http://mywebservice/v6" xmlns:thirdparty="http://mywebservice/v6/thirdparty/v6">
         <return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="status:agreementstatus">
            <status:level>IN-PROGRESS</status:level>
            <status:nextWorkDate>2020-07-31T09:36:50+01:00</status:nextWorkDate>
            <status:type>788</status:type>
            <status:agreementNumber>89ADFGH</status:agreementNumber>
         </return>
      </ns0:liststatusResponse>
   </S:Body>
</S:Envelope>

我需要提取几个值,例如第一个 &lt;return... 块中的status:type 中的788

我有一个用于检查响应返回值的测试实用程序:

@Test
public static void xmlPathTester() {
  XmlPath xmlPath = new XmlPath(XML);
  List<String> results = xmlPath.getList("S:Envelope.S:Body.ns0:liststatusResponse.return.status:type.text()");
    for (String result : results) {
        System.out.println(result);
    }
}

但这目前返回 1 个结果 ~ 一个空字符串。

我不清楚我哪里出错了。

【问题讨论】:

  • 您提供的 XML 会引发错误
  • @Fenio 谢谢,确实如此。但是,现在已修复并收到正确的响应,但我仍然无法获得 789&lt;status:type&gt; 属性
  • 当我将给定的 XML 粘贴到我的代码中时,我仍然遇到 XML 解析错误
  • @Fenio 道歉 :( 现已修复!

标签: java xml xpath soap rest-assured


【解决方案1】:

当你使用 XmlPath 时,不要提供命名空间。

XmlPath path = XmlPath.from(xml);
    path.getList("Envelope.Body.liststatusResponse.return.type.text()").forEach(System.out::println);

此代码返回:

788

为了声明命名空间,您必须根据documentation 使 XmlPath 命名空间感知:

given().
        config(RestAssured.config().xmlConfig(xmlConfig().with().namespaceAware(true))).
when().
         get("/package-db-xml").
then().
         body(hasXPath("/db:package-database", namespaceContext));

由于您有多个命名空间,我将只使用非命名空间 XmlPath

【讨论】:

    猜你喜欢
    • 2019-08-21
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多