我找到了几种将SOAP响应转换为对象的解决方案,但它们都不起作用。我将我的soap响应作为xml字符串,例如:
$response = '
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://xxx.company.com" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:DataResponse>
<DataReturn xsi:type="ns1:OutXT">
<Errors SOAP-ENC:arrayType="ns1:Errors[12]" xsi:type="ns1:ErrorsArray">
<item xsi:type="ns1:Errors">
<ErrorItem xsi:type="xsd:string">Access</ErrorItem>
<DescErrors xsi:type="xsd:string">User not found</DescErrors>
</item>
<item xsi:type="ns1:Errors">
<ErrorItem xsi:type="xsd:string">Client check</ErrorItem>
<DescErrors xsi:type="xsd:string">Report 85 completed</DescErrors>
</item>
</Errors>
<ClientCategory xsi:type="xsd:string">A</ClientCategory>
<Premium xsi:type="ns1:Premium">
<Price xsi:type="xsd:int">2509</Price>
<Coef xsi:type="xsd:float">4.558</Coef>
<Discount xsi:type="xsd:int">35</Discount>
</Premium>
<Sample xsi:type="xsd:int">0</Sample>
<Status xsi:type="xsd:string">OK</Status>
</DataReturn>
</ns1:DataResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
我需要摆脱所有名称空间,包括冒号和所有属性,所以在这种情况下的结果将是:
<Envelope>
<Body>
<DataResponse>
<DataReturn>
<Errors>
<item>
<ErrorItem>Access</ErrorItem>
<DescErrors>User not found</DescErrors>
</item>
<item>
<ErrorItem>Client check</ErrorItem>
<DescErrors>Report 85 completed</DescErrors>
</item>
</Errors>
<ClientCategory>A</ClientCategory>
<Premium>
<Price>2509</Price>
<Coef>4.558</Coef>
<Discount>35</Discount>
</Premium>
<Sample>0</Sample>
<Status>OK</Status>
</DataReturn>
</DataResponse>
</Body>
</Envelope>
我找到了一些据称可以完成这项工作的正则表达式,但它根本不起作用:
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
答案 0 :(得分:0)
那么你可以用空字符串替换这个表达式的匹配:
(((?<=<\/)|(?<=<))[^\/\s]*?:|\s?[^\s]+=".*?")
此表达式匹配
(?<=<)|(?<=<\/))[^\/\s]*?:
启动和关闭xml标签的命名空间(<
或</
之后的所有内容,直至包含:
)\s?[^\s]+=".*?"
基本上所有xml属性及其前置空格