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>[email protected]</name>
<email>[email protected]</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='[email protected]' 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='[email protected]'/>
</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];
?>
4
  • Getting at namespaced children isn't that straightforward with SimpleXML. Have a look at this tutorial which explains how you can access them using the children() method. Commented May 11, 2011 at 15:34
  • You really should look into solving this via XPath, indexing into the XML with square brackets is cumbersome and not very readable. Commented May 11, 2011 at 15:57
  • I've noticed that the URL 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: blogs.sitepoint.com/simplexml-and-namespaces Commented May 11, 2011 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? Commented May 12, 2011 at 8:19

1 Answer 1

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'));
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.