php simplexml获取具有特定标记的所有元素

时间:2014-02-01 20:10:35

标签: php xml simplexml

<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列出它们。 很抱歉没有细节,这很快。

1 个答案:

答案 0 :(得分:2)

就是这样:

$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