1

I have been trying to extract data from this XML file from Google Calendar with not much success with the namespaced stuff.

It is just the gd and gCal data I am having problems with - I've searched all over and haven't been able to get it to work :(

Any help appreciated! :)

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005' xmlns:gd='http://schemas.google.com/g/2005'>
<id>http://www.google.com/calendar/feeds/abc%40group.calendar.google.com/private-abc/full/abc</id>
<published>2011-05-03T07:45:56.000Z</published>
<updated>2011-05-03T07:45:56.000Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/>
<title type='text'>title</title>
<content type='text'>content</content>
<link rel='alternate' type='text/html' href='http://www.google.com/calendar/event?eid=abc' title='alternate'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/calendar/feeds/abc%40group.calendar.google.com/private-abc/full/abc'/>
<author>
<name>my@email.com</name>
<email>my@email.com</email>
</author>
<gd:comments>
<gd:feedLink href='http://www.google.com/calendar/feeds/abc%40group.calendar.google.com/private-abc/full/abc/comments'/>
</gd:comments>
<gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'/>
<gd:where/>
<gd:who email='abc@group.calendar.google.com' rel='http://schemas.google.com/g/2005#event.organizer' valueString='TV2'/>
<gd:when endTime='2011-05-10T22:50:00.000+01:00' startTime='2011-05-10T22:25:00.000+01:00'/>
<gd:transparency value='http://schemas.google.com/g/2005#event.opaque'/>
<gCal:anyoneCanAddSelf value='false'/>
<gCal:guestsCanInviteOthers value='true'/>
<gCal:guestsCanModify value='false'/>
<gCal:guestsCanSeeGuests value='true'/>
<gCal:sequence value='0'/>
<gCal:uid value='abc@google.com'/>
</entry>

And the PHP code so far is:

<?php
$source = "data2.xml";
$xmlstr = file_get_contents($source);
$parseXMLFile = new SimpleXMLElement($xmlstr);
echo $parseXMLFile->id[0];
echo "<br>";
echo $parseXMLFile->title[0];
echo "<br>";
echo $parseXMLFile->content[0];
?>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
fencin
  • 11
  • 1
  • 3
  • You really should look into solving this via XPath, indexing into the XML with square brackets is cumbersome and not very readable. – Tomalak May 11 '11 at 15:57
  • I've noticed that the URL http://schemas.google.com/gCal/2005 which is in the top line of the xml file doesn't exist anymore? It says Server Not Found - could this be the problem? reason I ask is because this worked on their example, but not mine: http://blogs.sitepoint.com/simplexml-and-namespaces/ – fencin May 11 '11 at 16:07
  • Namespaces are just unique strings, really (just like in other languages). They can be almost anything, but often they are in URL form. That doesn't mean the URL has to exist, it's just a convention. Have you had a look at the question @Gordon linked to? – Tomalak May 12 '11 at 08:19
  • Getting at namespaced children isn't that straightforward with SimpleXML. Have a look at [this tutorial](http://blogs.sitepoint.com/simplexml-and-namespaces/) which explains how you can access them using the `children()` method. – James C May 11 '11 at 15:34

1 Answers1

1

Checkout this answer: https://stackoverflow.com/a/622363/195835

Basically it recommends using an xPath call, eg.

$xml = new SimpleXMLElement($r);

foreach($xml->xpath('//event:event') as $event) {
    var_export($event->xpath('event:sessionKey'));
}
Community
  • 1
  • 1
Simon East
  • 55,742
  • 17
  • 139
  • 133