解析谷歌日历的XML源数据

5

我从公共谷歌日历中获取了XML订阅。看起来像这样:

<?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='.........@google.com'/>
            <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变量中没有这个节点,如何获取它?
4个回答

10

1
哦,这太棒了。谢谢你向我展示这个库 :) - 6bytes
哈哈,不客气!很棒,是吧? - Kenneth Reitz
抱歉这是老的了,但你能解释一下你如何让它正常工作吗?我有类似的代码和和你一样的问题,但我不太确定如何使用coreylib。谢谢! - iHeff
昨天花了一整天的时间研究Google日历API,最终得出结论:由于我只需要收集数据而不需要其他功能,所以它太复杂了。然后我用另一种方法在1分钟内完成了任务...太棒了! - Hrvoje Golcic

5
当然,解析XML有几种方法。使用XPath在PHP网站上显示Google日历事件(请参见“使用PHP解析Google日历源”)和将您的PHP应用程序与Google日历集成是两个综合资源,提供示例代码等内容。
个人而言,我采用了以下方法:
$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";
}

1
你可以使用Zend GData库来解析Google Web服务(包括日历)。这比手动使用XML代码更容易。有一个教程向你展示如何做到这一点在这里

0

2
这个问题被标记为PHP,所以如果你甚至没有说明它是jQuery的替代方案,那么你的jQuery解决方案就是不合适和误导的。 - wdyp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接