PHP xml数组解析数据
问题描述:
嘿,我所有的这种类型的XML我试图从中获取数据。这仅仅是一个大的XML代码剪断:PHP xml数组解析数据
<entry>
<id>http://www.google.com/calendar/feeds/[Letters/numbers here]group.calendar.google.com/public/basic/[Letters/numbers here]</id>
<published>2013-08-01T13:40:24.000Z</published>
<updated>2013-08-01T13:40:24.000Z</updated>
<title type='html'>[Title Here]</title>
<summary type='html'>When: Tue Sep 24, 2013 7am</summary>
<content type='html'>When: Tue Sep 24, 2013 7am
<br />Event Status: confirmed
</content>
<link rel='alternate' type='text/html' href='https://www.google.com/calendar/event?eid=[Letters/numbers here]' title='alternate'/>
<link rel='self' type='application/atom+xml' href='https://www.google.com/calendar/feeds/[Letters/numbers here]group.calendar.google.com/public/basic/[Letters/numbers here]'/>
<author>
<name>[email here]</name>
<email>[email here]</email>
</author>
</entry>
etc... etc....
目前我能得到这两个发表和更新只是做以下罚款:
<?php
$url = strtolower($_GET['url']);
$doc = new DOMDocument();
$doc->load('http://www.google.com/calendar/feeds/[number/letters here].calendar.google.com/public/basic');
$entries = $doc->getElementsByTagName("entry");
foreach ($entries as $entry) {
$tmpPublished = $entry->getElementsByTagName("published");
$published = $tmpPublished->item(0)->nodeValue;
$tmpUpdated = $entry->getElementsByTagName("updated");
$updated = $tmpUpdated->item(0)->nodeValue;
}
?>
但是我不能确定为如何从父数组中获取内部数据 - 在这种情况下是链接。
,所以我需要得到
链路>的href
我可以想象这将是:
$tmpLink = $entry->getElementsByTagName("link");
$link = $tmpLink->item(2)->nodeValue;
任何帮助将是巨大的!
答
你可以使用:
$links = $doc->getElementsByTagName("link");
foreach ($links as $link) {
$href = $link->getAttribute("href");
}
,如果你想获得的href ......希望我理解你想:)
答
你可以用simplexml_load_string
做到这一点像下面的代码是什么:
$entries = simplexml_load_string($string);
foreach ($entries as $entry) {
echo $entry->published;
echo $entry->updated;
foreach($entry->link as $link)
{
echo $link->attributes()->type;
echo $link->attributes()->rel;
}
}
你有没有想过使用xpath? [相关SO问题和答案](http://*.com/questions/1288940/domdocument-xpath-html-tag-of-each-node) – miah