2012-04-17 44 views
1

我有以下的XML文件(下面),我想獲得根節點名稱的鉤子。如何使用PHP在XML文件根節點上獲取鉤子?

我已經試過如下:

$config = simplexml_load_string($xml_data); 
echo $config->getName(); 

呼應的什麼都沒有。我也重複了結果,但它開始於根節點的子節點

foreach($config as $key => $value) 
{ 
    echo $key; 
} 
// echo's ReturnStatus, SearchURL and PropertyResults 

我只想要根節點「SearchResponse」的名稱。我似乎無法找到的SimpleXMLElement類文檔中任何會幫助http://www.php.net/manual/en/class.simplexmlelement.php

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<SearchResponse> 
    <ReturnStatus> 
    <Success> 
     <data>true</data> 
     <data2>true</data2> 
    </Success> 
    <Success> 
     <data>false</data> 
     <data2>true</data2> 
    </Success> 
    <Exception /> 
    </ReturnStatus> 
    <SearchURL /> 
    <PropertyResults> 
    <PropertyResult> 
     <PropertyID>1468830</PropertyID> 
     <PropertyName>Sands Acapulco</PropertyName> 
    </PropertyResult> 
    <PropertyResult> 
     <PropertyID>1353302</PropertyID> 
     <PropertyName>Acapulco Turquesa</PropertyName> 
    </PropertyResult> 
    <PropertyResult> 
     <PropertyID>4521565</PropertyID> 
     <PropertyName>Almaria Delsonto</PropertyName> 
    </PropertyResult> 
    </PropertyResults> 
</SearchResponse> 
+2

您的代碼[適合我](http://codepad.org/ggvVydOF)... – DaveRandom 2012-04-17 16:48:48

+1

對我而言,它也可以工作 – 2012-04-17 16:48:59

+0

是的,你的權利,這個工作,我忘了包括的一件事是Zend_Config_Xml抽象我正在使用。這似乎限制了你無法訪問根節點。我會拋棄它並堅持原生PHP。謝謝。 – 2012-04-17 16:57:08

回答

2

這並不工作:

$config = simplexml_load_string($xml); 
echo $config->getName(); 

與XML測試你提供的,輸出:

SearchResponse 

你寫它不會工作,所以可能這個問題不是API。