2013-09-01 37 views
1

我想執行以下代碼:調用未定義方法的SimpleXMLElement :: registerXPathNamespace()

<?php 
$xml = <<<EOD 
<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns="urn:enterprise.soap.sforce.com" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <loginResponse> 
      <result> 
       <metadataServerUrl> 
        https://na3.salesforce.com/services/Soap/m/28.0/00D50000000IdrE 
       </metadataServerUrl> 
       <passwordExpired>false</passwordExpired> 
       <sandbox>false</sandbox> 
       <serverUrl> 
        https://abc.com/services/Soap/c/28.0/00D50000000IdrE 
       </serverUrl> 
       <sessionId> 
        00D50000000IdrE!AREAQK4VYXRaHoL_uRvOi.QXXw3ahAt2Cge254wygiW7cr_f6DVa2pDC6g57w5IE 
        fidAu3ZRsJFBN5Bwb6DVhF18zKFiVVyT 
       </sessionId> 
       <userId>00550000001Dd4uAAC</userId> 
       <userInfo> 
        <accessibilityMode>false</accessibilityMode> 
        <currencySymbol>$</currencySymbol> 
        <orgAttachmentFileSizeLimit>5242880 
        </orgAttachmentFileSizeLimit> 
        <orgDefaultCurrencyIsoCode>USD</orgDefaultCurrencyIsoCode> 
        <orgDisallowHtmlAttachments>false 
        </orgDisallowHtmlAttachments> 
        <orgHasPersonAccounts>false</orgHasPersonAccounts> 
        <organizationId>00D50000000IdrEEAS</organizationId> 
        <organizationMultiCurrency>false</organizationMultiCurrency> 
        <organizationName>3CLogic</organizationName> 
        <profileId>00e500000017al5AAA</profileId> 
        <roleId xsi:nil="true"/> 
        <sessionSecondsValid>7200</sessionSecondsValid> 
        <userDefaultCurrencyIsoCode xsi:nil="true"/> 
        <userEmail>[email protected]</userEmail> 
        <userFullName>Ramana</userFullName> 
        <userId>00550Dd4uAAC</userId> 
        <userLanguage>en_US</userLanguage> 
        <userLocale>en_US</userLocale> 
        <userName>[email protected]</userName> 
        <userTimeZone>America/New_York</userTimeZone> 
        <userType>Standard</userType> 
        <userUiSkin>Theme3</userUiSkin> 
       </userInfo> 
      </result> 
     </loginResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 
EOD; 
$sxe = new SimpleXMLElement($xml); 
$sxe->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/ '); 
$sxe->registerXPathNamespace('def', 'urn:enterprise.soap.sforce.com'); 
$result = $sxe->xpath('/env:Envelope/env:Body/def:loginResponse/def:result/def:serverUrl'); 
?> 

然而,得到以下錯誤:

Call to undefined method SimpleXMLElement::registerXPathNamespace() 

我使用PHP 5.0.5版。但是,PHP版本5.1.0和5.5.3我得到這個錯誤,但也沒有輸出。我正在嘗試提取xml標記'serverUrl'的值。 請幫忙。如果可能的話,請爲php 5.0.5建議解決方案

+2

除非您使用PHP 5.2.0或更高版本,否則請勿觸摸它。 [SimpleXMLElement :: registerXPathNamespace參考](http://php.net/manual/en/simplexmlelement.registerxpathnamespace.php)標題下的第一行:'(PHP 5> = 5.2.0)' – Jason

+0

謝謝......你能建議我在這種情況下提取一些解決方案來提取xml標記值 – hemanonearth

+0

PHP 5.0已[幾乎完全不支持8年](http://www.php.net/eol.php)。對於這種舊版本的*任何*問題,最好的解決方案是更新到支持的版本。我意識到這並不總是可能的,但是你肯定需要非常清楚使用這樣一箇舊版本的侷限性,並且只使用你在該版本中存在的特性。 – IMSoP

回答

0

XPath功能僅在PHP 5.2.0(本身爲more than 2 and a half years beyond "End of Life")中添加到SimpleXML中,因此如果您真的堅持使用PHP的5.0版本.5,你必須想出一個不同的解決方案。

幸運的是,XPath表達式非常基本,除了使用名稱空間以外,->children() method自PHP 5.0.1起就能夠處理這些表達式。

此代碼應細,因此在PHP的任何版本,因爲然後運行:

$sxe = new SimpleXMLElement($xml); 

// Define some short identifiers for namespaces without relying on their prefix in the XML 
define('NS_ENV', 'http://schemas.xmlsoap.org/soap/envelope/'); 
define('NS_SFORCE', 'urn:enterprise.soap.sforce.com'); 

// $sxe already represents the soapenv:Envelope node, so we don't need to mention that 
$result = $sxe 
    ->children(NS_ENV)->Body 
    ->children(NS_SFORCE)->loginResponse->result->serverUrl; 

// $result is now the SimpleXML object for the `serverUrl` node(s) 
// (unlike with ->xpath(), which returns a plain PHP array of "search results") 
echo trim($result); 

這裏的a live sample顯示它在各種不同版本的PHP評價。

相關問題