duangouhui0446 2011-05-11 15:28
浏览 41

使用php [duplicate]解析xml命名空间

This question already has an answer here:

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];
?>
</div>
  • 写回答

1条回答 默认 最新

  • dp411805872 2012-09-28 00:26
    关注

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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大