2009-12-29 76 views
1

我可能有一個簡單的問題, 我需要知道怎麼去嵌套的命名空間屬性/像下面訪問命名空間中的元素/屬性用SimpleXML

<gf:marketValue> 
    <gd:money amount='150990.0' currencyCode='USD'/> 
    </gf:marketValue> 

這個元素是從google-api

而且,我現在不知道至極一個我應該使用 網址... /組合 或 組合/ 1 /位置 得到stockquotes

所以,我對此可能是錯的。 (上面的XML是從產品線)在進階

$response= simplexml_load_string($response); 
foreach($response->entry as $entry) 
{ 
$ns_gf = $entry->children('http://schemas.google.com/finance/2007'); 

感謝,理查德

回答

2
$response= simplexml_load_string($response); 

$entry_data = $response -> xpath("//positionData"); 

foreach($entry_data as $data) 
{ 
echo $data["shares"] ." <br />"; 

或者,你可以用這個去的,這將回聲出的所有數據和上面的符號:

$entries = $response -> xpath("//entry"); 

foreach($entries as $entry) { 
    echo $entry->symbol['symbol']."<br />"; 
    foreach($entry -> positionData -> attributes() as $att_name => $att_value) { 
     echo $att_name. " = ". $att_value."</br>"; 
    } 
} 
+0

謝謝,所以兩者都是xpath,它是否也可以與children()一起使用,也可以在sitenote上使用。我是否從股票或市場價格獲得不同股票的報價? – Richard 2009-12-29 14:33:59

+0

用xpath獲得了一個空白屏幕? – Richard 2009-12-29 14:59:26

+1

對不起,它應該是'xpath(「// gf:entry」)' – Anthony 2009-12-29 15:27:51