2009-08-28 103 views
6

我从公开的Google日历获取XML源。看起来是这样的:解析Google日历XML Feed

<?xml version='1.0' encoding='UTF-8'?> 
    <feed xmlns='................' xmlns:gd='http://schemas.google.com/g/2005'> 
     <id>http://www.google.com/calendar/feeds/........./public/full</id> 
     <updated>2009-08-24T10:57:00.000Z</updated> 
     <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/> 
      <title type='text'>Sports Events</title> 
    ..... 
     <entry> 
      <id>...........</id> 
      <published>2009-08-14T00:29:58.000Z</published> 
      <updated>2009-08-14T00:29:58.000Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/> 
.............. 
      <georss:where> 
       <gml:Point> 
        <gml:pos>11.111111 -22.22222</gml:pos> 
       </gml:Point> 
      </georss:where> 
      <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'/> 
      <gd:transparency value='http://schemas.google.com/g/2005#event.transparent'/> 
      <gCal:uid value='[email protected]'/> 
      <gCal:sequence value='0'/> 
      <gd:when startTime='2009-10-03' endTime='2009-10-04'/> 

............. 

我们PHP:

$calendar = simplexml_load_file('http://my.feed.com'); 
foreach ($calendar->entry as $item) { 
    //here I get the whole <entry> node 
    $gd = $item->children('http://schemas.google.com/g/2005'); 
    // now in $gd I have all nodes like <gd:XXXXX> 
} 

的问题是如何从这里获得价值?

<gml:pos>11.111111 -22.22222</gml:pos> 

它没有出现在我的$ gd变量中,如何获得这个节点?

回答

10

我极力推荐使用此图书馆:https://github.com/collegeman/coreylib。它会让所有事情从头到尾变得简单易行。

+1

哦,这太棒了。谢谢你给我看这个图书馆:) – 6bytes 2009-08-28 15:52:04

+0

哈哈我的荣幸!这很好,不是吗? – 2009-08-28 21:45:22

+0

这是非常令人印象深刻的 – 2009-12-19 15:18:49

1

您可以使用Zend GData库来解析Google网络服务(包括日历)。这比尝试手动使用XML代码更容易。有一个教程,告诉你如何做到这一点here

+0

感谢但coreylib工作像魔术:) – 6bytes 2009-11-18 11:24:00

+0

你有一个新的链接? – Zoredache 2012-06-15 17:13:08

5

当然有几种解析XML的方法。 Display Google Calendar events on your PHP Web site with XPath(请参阅“使用PHP解析Google日历Feed”)和Integrate your PHP application with Google Calendar是包含示例代码等的两个全面资源。

就个人而言,我用下面的方法:

$doc = new DOMDocument(); 
$doc->load('http://my.feed.com'); 
$entries = $doc->getElementsByTagName("entry"); 
foreach ($entries as $entry) { 
    $titles = $entry->getElementsByTagName("title"); 
    $title = $titles->item(0)->nodeValue; 

    $times = $entry->getElementsByTagName("when"); 
    $startTime = $times->item(0)->getAttributeNode("startTime")->value; 
    $when = date("l jS \o\f F Y - h:i A", strtotime($startTime)); 
    // ... 
} 

为了访问的GeoRSS命名空间等等看看(及其输出)

foreach ($doc->getElementsByTagNameNS('*', '*') as $element) { 
    echo 'localName: ', $element->localName, ', prefix: ', $element->prefix, "\n"; 
} 
0

完整的日历是远远超过更容易Coreylib。 Coreylib不是简单的下降,尽管我确信它非常强大。

我的FullCalendar在5分钟内运行。 http://arshaw.com/fullcalendar/docs/google_calendar/

+2

这个问题被标记为php,所以如果你甚至没有说它是jQuery的替代品,那么你的jQuery解决方案是不合理的和误导性的。 – wdyp 2013-08-14 14:21:44