PHP、XML 元素路径

嘿,我正在尝试从 XML 文件中获取查看者。问题出在路径上,因为其他路径对我有用。我想问题是因为它是元素?<media:statistics views="131"/>


$url = "https://www.youtube.com/feeds/videos.xml?channel_id=UCH7Hj6l_xDmbyvjQOA5Du0g";

$xml = simplexml_load_file($url);


$views = $xml->entry[0]->children('media', true)->group[0]->children('media', true)->community[0]->children('media', true)->attributes('statistics');

echo $views;


哈士奇WWW
浏览 92回答 1
1回答

达令说

您可以通过使用而不是首先使用统计属性来获取视图->attributes('statistics'),然后从属性中获取视图:->statistics->attributes()->views;代码可能如下所示:$url = "https://www.youtube.com/feeds/videos.xml?channel_id=UCH7Hj6l_xDmbyvjQOA5Du0g";$xml = simplexml_load_file($url);$views = $xml->entry[0]->children('media', true)->group[0]->children('media', true)->community[0]->children('media', true)->statistics->attributes()->views;echo $views;输出131
打开App,查看更多内容
随时随地看视频慕课网APP