php纯单纯性获得具有特定标签的所有元素


<playlist revision="1">
(...)
<relatedLink>
<id>tag:bbc.co.uk,2008:iplayer:concept_pid:b03tcb0b</id>
<title>MI High: Series 7: The Man Who Drew Tomorrow</title>
<link rel="alternate" href="http://www.bbc.co.uk/iplayer/episode/b03tcb0b/MI_High_Series_7_The_Man_Who_Drew_Tomorrow/"/><link rel="thumb" href="http://ichef.bbci.co.uk/programmeimages/p01q7ysm/b03tcb0b_150_84.jpg" type="image/jpeg" width="150" height="84"/><summary>Children's spy drama. When clairvoyant Derren Beige is kidnapped by KORPS, the spies must race against the clock to decipher the mystery of his ability.</summary>
</relatedLink><relatedLink><id>tag:bbc.co.uk,2008:iplayer:concept_pid:b03qgljm</id><title>MI High: Series 7: The Mayze</title><link rel="alternate" href="http://www.bbc.co.uk/iplayer/episode/b03qgljm/MI_High_Series_7_The_Mayze/"/><link rel="thumb" href="http://ichef.bbci.co.uk/programmeimages/p01phbl5/b03qgljm_150_84.jpg" type="image/jpeg" width="150" height="84"/><summary>Children's spy drama. It's a new term at St Hearts and Frank tasks the elite spies with the mission of tracking down one of Zoe's remaining duplicants.</summary></relatedLink>

我需要获取所有相关链接,并使用PHP SimplexML列出它们。抱歉缺乏细节,这很快。

这就是:

$xml = simplexml_load_string($x); // assume XML in $x
foreach ($xml->relatedLink as $rl) {
    echo $rl->title . PHP_EOL;
}

看到它工作:https://eval.in/97004

阅读手册:http://www.php.net/manual/en/simplexml.examples-basic.php

xpath也是一个不错的选择,如果您想选择所有出现,无论在树上的位置如何。此作弊表可以帮助您表达表达。

$xml=simplexml_load_string($x);
    
foreach($xml->xpath(".//relatedLink") as $rl){
 print_r($rl);
}

最新更新