如何从SOAP请求解析返回的XML - jQuery

时间:2018-03-21 17:15:17

标签: jquery xml soap

我之前没有以这种格式处理过xml,没有标签名称。可能会返回更多或更少的节点,因此我无法计算它们。之后可能会有一两个,所以我无法计算结束。我需要找到谁的价值是结果。从那里找到了文字。

返回的数据是:

<pre><?xml version="1.0" encoding="utf-8"?>
    <soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns1:demosetupResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://tempuri.org/">
    <demosetupReturn xmlns:ns2="http://xml.apache.org/xml-soap" xsi:type="ns2:Map">
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
      <key xsi:type="soapenc:string">DemoUName</key>
      <value xsi:type="soapenc:string"></value>
    </item>
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
      <key xsi:type="soapenc:string">DemoPass</key>
      <value xsi:type="soapenc:string"></value>
    </item>
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
      <key xsi:type="soapenc:string">FutAcct</key>
      <value xsi:type="soapenc:string"></value>
    </item>
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
      <key xsi:type="soapenc:string">FxAcct</key>
      <value xsi:type="soapenc:string"></value>
    </item>
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
      <key xsi:type="soapenc:string">UUID</key>
      <value xsi:type="soapenc:string"></value>
    </item>
    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
      <key xsi:type="soapenc:string">Result</key>
      <value xsi:type="soapenc:string">Error: Duplicate user account!</value>
    </item>
  </demosetupReturn>
</ns1:demosetupResponse>

我的PHP cURL脚本的相关部分:

f(curl_exec($soap_do) === false) {
        $err = 'Curl error: ' . curl_error($soap_do);
        curl_close($soap_do);
        print $err;
    } else {
        $result = curl_exec($soap_do);
        echo '<pre>';
        print_r($result);
        // print_r($xml_post_string);
    }
    curl_close($soap_do);

1 个答案:

答案 0 :(得分:1)

您可以使用XPath来提取SOAP消息的主体(我认为在这种情况下它是<demosetupReturn xml...元素。然后使用SimpleXML迭代<item>元素并创建输出键/值对的数组。

$xml = simplexml_load_string($result);
$body = $xml->xpath("//soapenv:Body//demosetupReturn")[0];
$output = [];
foreach ( $body as $item)  {
    $output[(string)$item->key] = (string)$item->value;
}
print_r($output);
相关问题