2011-06-22 71 views
2

我有一個來自Google Contacts API的xml提要,但我無法閱讀<gd:標籤。Google Contacts用PHP解析PHP XML

$xml = simplexml_load_string($google_contacts); 

這會讀取除包含電子郵件信息的<gd:標記之外的所有元素。

什麼是最好的事情?

+0

我會繼續並使用不同的XML解析器。將當前xml與對象解析器混合在一起可能會讓事情變得醜陋。 – FinalForm

+0

改爲使用DomDocument,它支持xml命名空間。 – hakre

+0

請在您嘗試訪問這些元素的位置顯示一些代碼。 *不讀*具體是指什麼? – hakre

回答

1

更新:如果您使用的是名稱空間和xpath,則您可能在SimpleXML中使用need to register them first

如果您在訪問元素時遇到問題,請嘗試將其包含在{}角度的括號內,這可能有助於:$xml->{gd:...}


如果我沒有記錯,SimpleXML對XML命名空間沒有/有限的支持。 DomDocument有,所以採取它。

第一啓動:

$dom = new DomDocument; 
$dom->loadXML($google_contacts); 
... 

或者使用的DomDocument因爲這是很難只SimpleXML來做到部分:

$domElement = dom_import_simplexml($simpleXmlNode); 
1

你可以閱讀 「GD:」 使用xml_parse標籤。它返回數組中的所有結果,作爲XML字符串不是由您共享的,所以你需要編寫進一步的邏輯,從陣列獲取數據

<?php 
$content = $google_contacts; 
$parser = xml_parser_create(); 
xml_parse_into_struct($parser, $content, $data, $index); 
xml_parser_free($parser); 
print"<pre>"; 
print_r($data); 
/*foreach($data as $vals) 
{  
    //write your code to get result from array 
}*/ 
?> 

http://www.php.net/manual/en/function.xml-parse.php

0

我有可能不能訪問<gd:同樣的問題節點。 我使自己的解決方案,我需要在每個條目下的聯繫人信息的全部列表。 我想這是因爲大腸的「:」在每個<gd:標籤則沒有取代它「:」這裏是我做過什麼:

$search=array('<gd:','</gd:'); 
$replace=array('<gd','</gd'); 
/*Create json object from $google_contacts(atom xml string) 
by replacing "gd:" to "gd"*/ 
$response = json_encode(str_replace($search, $replace,$google_contacts)); 
/*Create Array from $response*/ 
$contactsList = json_decode($response, true); 
print "<pre>" . print_r($contactsList['entry'], true); 

這會使下面的谷歌聯繫人的xml:

<? xml version = '1.0' encoding = 'UTF-8' ?> 
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'> 

    ... 

    <entry> 
     <id>http://www.google.com/m8/feeds/contacts/xxxxxxxx%40gmail.com/base/0</id> 
     <updated>2012-09-23T01:54:56.646Z</updated> 
     <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
     <title type='text'>Will Jordan</title> 
     <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSXN'/> 
     <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0'/> 
     <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0'/> 
     <link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1348365xxxxxx'/> 
     <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     <gd:email rel='http://schemas.google.com/g/2005#home' address='[email protected]'/> 
     <gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>8888888888</gd:phoneNumber> 
     <gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'>9999999999</gd:phoneNumber> 
     <gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>5555555555</gd:phoneNumber> 
     <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/4fde39d984'/> 
     <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/3f5dc5d3be0'/> 
    </entry> 
</feed> 

到陣列的這個print_r的輸出:

array(
[id] => http://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/base/0 
    [updated] => 2013-08-27T19:33:22.431Z 
    [category] => Array(
     [@attributes] => Array(
      [scheme] => http://schemas.google.com/g/2005#kind 
      [term] => http://schemas.google.com/contact/2008#contact 
     ) 
    ) 
    [title] => Will Jordan 
    [link] => Array(
     [0] => Array(
      [@attributes] => Array(
       [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo 
       [type] => image/ * 
       [href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSX 
      ) 
     ) 
     [1] => Array(
      [@attributes] => Array(
       [rel] => http://schemas.google.com/contacts/2008/rel#photo 
       [type] => image/ * 
       [href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0 
      ) 
     ) 
     [2] => Array(
      [@attributes] => Array(
       [rel] => self 
       [type] => application/atom+xml 
       [href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0 
      ) 
     ) 
     [3] => Array(
      [@attributes] => Array(
       [rel] => edit 
       [type] => application/atom+xml 
       [href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1377632 
      ) 
     ) 
    ) 
    [gdemail] => Array(
     [0] => Array(
      [@attributes] => Array(
       [rel] => http://schemas.google.com/g/2005#other 
       [address] => [email protected] 
       [primary] => true 
      ) 
     ) 
     [1] => Array(
      [@attributes] => Array(
       [rel] => http://schemas.google.com/g/2005#home 
       [address] => [email protected] 
      ) 
     ) 
    ) 
    [gdphoneNumber] => Array(
     [0] => 8888888888 
     [1] => 9999999999 
     [2] => 5555555555 
    ) 
) 
1

的東西,我沒有讓它easer與我是通過附加得到了GDATA早在JSON工作10請求字符串

我的完整的查詢字符串看起來像:

https://www.google.com/m8/feeds/contacts/{userEmail}/full?oauth_token=token&max-results=9999999&alt=json'); 

然後,它只是常規的JSON。