我在使用PHP SimpleXMLElement
和simpleSMLToArray()
函数解析SOAP响应时遇到问题。我从SOAP服务器获得SOAP响应很好。在这种情况下,我正在编写SOAP客户端和服务器。我正在使用NuSoap作为服务器。对我来说,肥皂响应看起来很完美,但PHP5肥皂客户端似乎没有解析它。所以,和以前一样,我使用PHP.NET(http://php.net/manual/en/book.simplexml.php)的SimpleXMLElement
和函数simpleXMLToArray()
,但似乎无法获得数组。PHP解析SOAP响应问题 - SimpleXMLElement
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:eCaseWSDL">
<SOAP-ENV:Body>
<ns1:registerDocumentByPatientResponse xmlns:ns1="urn:eCaseWSDL">
<returnArray xsi:type="tns:ReturnResult">
<id xsi:type="xsd:int">138</id>
<method xsi:type="xsd:string">registerDocumentByPatient</method>
<json xsi:type="xsd:string">0</json>
<message xsi:type="xsd:string">success</message>
<error xsi:type="xsd:string">0</error>
</returnArray>
</ns1:registerDocumentByPatientResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
来自我的课程的PHP代码($ this references apply to my library code)。
// Define SimpleXMLElement
$xml_element = new SimpleXMLElement($response_string); // SOAP XML
$name_spaces = $xml_element->getNamespaces(true);
var_dump($name_spaces);
$soap = $xml_element->children('ns1');
var_dump($soap);
$soap_array = $this->simpleXMLToArray($soap);
var_dump($soap_array);
return $soap_array;
我可以看到命名空间; ns1等,但是,该数组不返回。 SimpleXMLElement
看起来像它正在返回一个对象,但是是空的。
<pre>array(3) {
["SOAP-ENV"]=>
string(41) "http://schemas.xmlsoap.org/soap/envelope/"
["ns1"]=>
string(13) "urn:eCaseWSDL"
["xsi"]=>
string(41) "http://www.w3.org/2001/XMLSchema-instance"
}
object(SimpleXMLElement)#23 (0) {
}
bool(false)
任何人有任何想法,我做错了什么。今天早上我一定不能喝够咖啡。我很想用正则表达式解析它。
不要... ...使用正则表达式。 .. argggg ... [**小马,他来了!**](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454 #1732454)另外,我相信这个答案可能会有所帮助... [使用SimpleXML解析名称空间的XML](http://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml) – rdlowrey 2012-02-13 17:35:54