2009-12-29 78 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