简体   繁体   English

如何使用放心从具有多个命名空间的 SOAP XML 响应中提取价值?

[英]How to extract value from SOAP XML response with multiple namespaces using Rest-assured?

I have the following SOAP Response returned from a call I make using Rest Assured:我有以下 SOAP 从我使用 Rest 进行的呼叫返回的响应:

<?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>

I need to extract several values, for example 788 in status:type in the first <return... block.我需要提取几个值,例如第一个<return...块中的status:type中的788

I have a test utility for checking returned values from the response:我有一个用于检查响应返回值的测试实用程序:

@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);
    }
}

But this currently returns 1 result ~ an empty String.但这目前返回 1 个结果 ~ 一个空字符串。

It's not clear to me where I am going wrong.我不清楚我哪里出错了。

When you use XmlPath, don't provide namespaces.使用 XmlPath 时,不要提供命名空间。

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

This code returns:此代码返回:

788

In order to declare namespaces you have to make XmlPath namespace aware as per documentation :为了声明命名空间,您必须按照文档使 XmlPath 命名空间感知:

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

and since you have multiple namespaces I would just use non-namespace XmlPath并且由于您有多个命名空间,我将只使用非命名空间 XmlPath

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

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