PHP 加载多个 XML 数据而不编号
PHP load multiple XML data without numbering
我在尝试从 XML 文件加载多个数据时遇到了一些问题。
我想加载所有 XML 数据,而不必将它们编号为 [0]、[1] 等。我该如何实现?
PHP
<?php
echo"<center>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
echo $xml->channels[0]->name . "<br>";
echo "<img class='responsive' src='";
echo $xml->channels[0]->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $xml->channels[0]->url;
echo "'><br>";
echo "<input type='submit' name='submit' value='PLAY CHANNEL'><br>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
echo $xml->channels[1]->name . "<br>";
echo "<img class='responsive' src='";
echo $xml->channels[1]->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $xml->channels[1]->url;
echo "'><br>";
echo "<input type='submit' name='submit' value='PLAY CHANNEL'><br>";
echo"</center>";
if(isset($_POST['url'])) {
shell_exec("sudo killall player.bin");
shell_exec("sudo player `youtube-dl -g ".($_POST['url'])." `>> /dev/null &");
}
?>
XML
<?xml version="1.0" encoding="utf-8"?>
<data>
<channels>
<banner>https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Sky-news-logo.svg/768px-Sky-news-logo.svg.png</banner>
<url>https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live</url>
</channels>
<channels>
<banner>https://lh3.googleusercontent.com/proxy/DqTJr6u-LpXfWyrJscDsoa_XdZsXLTm1PXDLw_eJYRAlwx0RtNO4thah_YQhKk-vWmeSHkIfSGsClFIQmTWDS0iNGM3VeeEXrSzYSVPRFyEf5NQ9TVw0B9KYXg</banner>
<url>https://www.youtube.com/channel/UCeY0bbntWzzVIaj2z3QigXg/live</url>
</channels>
</data>
如the examples in the PHP manual所示,您可以使用正常的foreach
循环遍历具有相同名称的元素:
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
foreach ( $xml->channels as $channelsElement ) {
echo $channelsElement->name;
}
我在尝试从 XML 文件加载多个数据时遇到了一些问题。 我想加载所有 XML 数据,而不必将它们编号为 [0]、[1] 等。我该如何实现?
PHP
<?php
echo"<center>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
echo $xml->channels[0]->name . "<br>";
echo "<img class='responsive' src='";
echo $xml->channels[0]->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $xml->channels[0]->url;
echo "'><br>";
echo "<input type='submit' name='submit' value='PLAY CHANNEL'><br>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
echo $xml->channels[1]->name . "<br>";
echo "<img class='responsive' src='";
echo $xml->channels[1]->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $xml->channels[1]->url;
echo "'><br>";
echo "<input type='submit' name='submit' value='PLAY CHANNEL'><br>";
echo"</center>";
if(isset($_POST['url'])) {
shell_exec("sudo killall player.bin");
shell_exec("sudo player `youtube-dl -g ".($_POST['url'])." `>> /dev/null &");
}
?>
XML
<?xml version="1.0" encoding="utf-8"?>
<data>
<channels>
<banner>https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Sky-news-logo.svg/768px-Sky-news-logo.svg.png</banner>
<url>https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live</url>
</channels>
<channels>
<banner>https://lh3.googleusercontent.com/proxy/DqTJr6u-LpXfWyrJscDsoa_XdZsXLTm1PXDLw_eJYRAlwx0RtNO4thah_YQhKk-vWmeSHkIfSGsClFIQmTWDS0iNGM3VeeEXrSzYSVPRFyEf5NQ9TVw0B9KYXg</banner>
<url>https://www.youtube.com/channel/UCeY0bbntWzzVIaj2z3QigXg/live</url>
</channels>
</data>
如the examples in the PHP manual所示,您可以使用正常的foreach
循环遍历具有相同名称的元素:
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
foreach ( $xml->channels as $channelsElement ) {
echo $channelsElement->name;
}