php simplexml 获取具有特定标签的所有元素MI High:系列 7:绘制明天的人

问题描述 投票:0回答:2
<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 列出它们。 抱歉缺乏细节,这很快。

php xml simplexml
2个回答
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


0
投票
如果您想选择所有出现的情况而不管树中的位置如何,

Xpath也是一个不错的选择。这个cheatsheet可以帮助你表达。

$xml=simplexml_load_string($x);
    
foreach($xml->xpath(".//relatedLink") as $rl){
 print_r($rl);
}
© www.soinside.com 2019 - 2024. All rights reserved.