开发者

parse xml namespace with php [duplicate]

This question already has answers here开发者_如何学C: Parse XML with Namespace using SimpleXML [duplicate] (6 answers) Closed 10 years ago.

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];
?>


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'));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜