2010-05-07 105 views
0

我正在使用PHP的SimpleXML從以下XML中獲取一些值;使用PHP從XML獲取屬性

- <entry> 
    <id>http://www.google.com/m8/feeds/contacts/email_address%40gmail.com/base/0</id> 
    <updated>2010-01-14T22:06:26.565Z</updated> 
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" /> 
    <title type="text">Customer Name</title> 
    <link rel="http://schemas.google.com/contacts/2008/rel#edit-photo" type="image/*" href="http://www.google.com/m8/feeds/photos/media/email_address%40gmail.com/0/34h5jh34j5kj3444" /> 
    <link rel="self" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/email_address%40gmail.com/full/0" /> 
    <link rel="edit" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/email_address%40gmail.com/full/0/5555" /> 
    <gd:email rel="http://schemas.google.com/g/2005#other" address="[email protected]" primary="true" /> 
    </entry> 

我可以得到標題:

$xml = new SimpleXMLElement($response_h1); 

foreach ($xml->entry as $entry) { 
    echo $entry->title, '<br />'; 
} 

可是如何才能讓地址= 「[email protected]」 屬性?

回答

1

您的名字空間無效。如果沒有命名空間,你應該可以像這樣獲取地址,

$email = (string)$xml->email['address']; 

SimpleXML中有一些錯誤。這些元素使用成員語法「 - >」,但只能使用數組語法「[]」訪問屬性。