SOAP信封解析与所有XMLNS - 不在Magento上工作
问题描述:
我已经尝试过,并且一直无法想出一个解决方案。我的问题是这样的: 我有一个SOAP信封响应是如下...SOAP信封解析与所有XMLNS - 不在Magento上工作
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:header>
<soapenv:body>
<mcu:prescreenusereligibilityresponse xmlns:mcu="http://www.ups.com/XMLSchema/XOLTWS/MCUserEligibility/v1.0">
<common:response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:responsestatus>
<common:code>1</common:code>
<common:description>Success</common:description>
</common:responsestatus>
</common:response>
<mcu:eligibilitystatuscode>3</mcu:eligibilitystatuscode>
</mcu:prescreenusereligibilityresponse>
</soapenv:body>
</soapenv:header>
</soapenv:envelope>
我然后访问我的Mac这样的元素:
$ns=array();
$xml=new SimpleXMLElement($string);
foreach($xml->getNamespaces(true) as $key=>$url){
$xml->registerXPathNamespace($key, $url);
$ns[]=strval($url);
}
print_r(strval($xml->children($ns[0])->header->body->children($ns[1])->prescreenusereligibilityresponse->eligibilitystatuscode));
用同样的方法在单独的Linux实例我在print_r
行发生错误,说最后一个孩子不能为空。我已确认这些值是正确的。我也尝试使用$xml->xpath('//mcu:eligibilitystatuscode')
没有成功。
我真的坚持-_-
答
我用了一个替代方案。 DOMDocument()
我注意到在将不同的对象节点打印到屏幕上后,xml字符串中的大小写是不同的。我尝试将名称空间与正确的大写字母进行比较,并且仍然没有使用SimpleXML成功。
$doc = new DOMDocument();
$doc->loadXML($result);
if($doc){
foreach($doc->getElementsByTagNameNS('*', '*') as $element){
if($element->tagName=='mcu:EligibilityStatusCode'){
if($element->nodeValue==0){
return true;
}
else{
return false;
}
}
}
return false;
}
else{
return false;
}
上面是工作代码,其中result是soap请求返回的xml。我的代码基本上循环通过xml响应中的每个节点。它依次显示在Magento前端的一个块上。
答
考虑,你说这适用于你的Mac,而不是在Linux上,它有可能是行结束的问题。在解析XML之前,尝试添加它以将所有可能的行结尾替换为正在运行的系统的默认行。
$string = preg_replace('~\R~u', PHP_EOL, $string);
同样的结果。我相当肯定'trim($ string)'会摆脱结束字符。 (我也试过) –
@DrewPeterson修剪只会在整个字符串的开始/结尾处删除空格,而不是每一行。 –