我正在尝试解析XML以获取"text"消息:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>Error saving JobsReport</faultstring>
<detail>
<ns2:ErrorMsg xmlns:ns2="http://www.testX.com.pl/wsdl/sdis-emm">
<Error code="20" text="More than one row with the given identifier was found: 389, for class: ekt.bean.sdis.RepPerson" />
</ns2:ErrorMsg>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
但是SimpleXMLElement()
和simplexml_load_file()
只返回空对象。当我删除"S:"是更好的,但不是ok。
registerXPathNamespace
不起作用
帮助,thx。
看起来像Soap响应。PHP自己的SoapClient对象自动处理这个XML,并像web服务的函数和类型定义中描述的那样,将对象作为响应交付。下面是一个小的说明性示例:
try {
$client = new SoapClient($wsdl, $options);
$response = $client->functionName($params);
var_dump($response);
// response would be an object with members as described by the webservice
} catch (SoapFault $e) {
// prints error message
echo $e->getMessage();
}
因此,为了更舒适,您绝对应该看看PHP手册中的SoapClient。SoapClient自动处理所有XML内容,因此您将不会接触到它。
如果您仍然希望自己处理XML请求和响应,您可以使用PHP自己的DomDocument对象来处理。下面是一个小的说明性示例:
$dom = new DomDocument();
$dom->loadXml($xmlResponse);
foreach ($dom->getElementsByTagNameNS('http://www.testX.com.pl/wsdl/sdis-emm', 'Error') as $element) {
echo $element->getAttribute('text');
}
这是一种更复杂的方法,因为如果发生错误,SoapClient会抛出SoapFault,您可以很容易地捕获该错误并对其进行任何处理。