get property from XML using PHP
I am using PHP's SimpleXML to get some values out of the following 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>
<开发者_Go百科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="customer@gmail.com" primary="true" />
</entry>
I can get the title with:
$xml = new SimpleXMLElement($response_h1);
foreach ($xml->entry as $entry) {
echo $entry->title, '<br />';
}
But how to get the address="customer@gmail.com" property?
Your namespace is invalid. If no namespace, you should be able to fetch the address like this,
$email = (string)$xml->email['address'];
There are some bugs in SimpleXML. The elements work with member syntax "->" but the attributes are only accessible using array syntax "[]".
精彩评论