2015-11-02 45 views
1

我有以下格式的響應PHP的SimpleXML獲得的數據

<?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="[email protected]"/> 
     </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; 
    } 
} 

我做的第一件事就是找一個錯,如果有,返回false。如果一切正常,我需要從xml中返回電子郵件地址。我曾嘗試沒有成功

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

我將如何去獲取電子郵件地址以下?

感謝

回答

1

電子郵件是「收件人」節點的屬性。

你可以試試這個:

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

http://php.net/simplexmlelement.attributes

+0

如果我這樣做,我得到兩個試圖讓非對象的屬性和調用一個成員函數屬性()非對象 –

+1

上哦,是的,對不起。 這是因爲de命名空間。試試這個: $ xml-> children('SOAP-ENV',true) - > Body-> children() - > ExecuteQueryResponse-> pOutput-> recipient-> attributes() - > http:// www.sitepoint.com/simplexml-and-namespaces/ – jolmos