PHP SimpleXML获取数据

PHP SimpleXML obtaining data

我有以下格式的回复

<?xml version='1.0'?>
<SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:myQyery' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
<SOAP-ENV:Body>
    <ExecuteQueryResponse xmlns='urn:xtk:myQyery' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
        <pOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
            <recipient email="myemail@email.com"/>
        </pOutput>
    </ExecuteQueryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

所以我做的第一件事就是加载字符串

public function checkResponse($serverResponse) {
    $xml = simplexml_load_string($serverResponse);

    if($xml->children('SOAP-ENV', true)->Body->Fault) {
        return false;
    }
    else {
        return true;
    }
}

我做的第一件事是查找错误,如果有错误,return 错误。如果一切正常,我需要 return 来自 xml 的电子邮件地址。我尝试了以下但没有成功

return $xml->children('SOAP-ENV', true)->Body->ExecuteQueryResponse->pOutput->recipient;

我将如何获取电子邮件地址?

谢谢

电子邮件是 'recipient' 节点的一个属性。

你可以试试这个:

return $xml->children('SOAP-ENV', true)->Body->ExecuteQueryResponse->pOutput->recipient->attributes()->email

http://php.net/simplexmlelement.attributes