简体   繁体   English

从xml文件创建html5播放列表

[英]Create a html5 playlist from an xml file

I am trying to create a HTML5 playlist using the audiojs plugin. 我正在尝试使用audiojs插件创建HTML5播放列表。 My playlist is in an external XML file as it is managed by a custom CMS: 我的播放列表位于外部XML文件中,因为它由自定义CMS管理:

<playlist>
   <item>
     <title>bla bla bla</title>
     <artist>Big Bla</artist>
     <path>/mp3/bla-bla-bla.mp3</path>
   </item>
   <item>
     <title>bla bla blab</title>
     <artist>lil Big Bla</artist>
     <path>/mp3/bla-bla-bla.mp3</path>
   </item>
</playlist>

This is my .php file: 这是我的.php文件:

        <div id="player-holder">
            <audio preload></audio>
            <ul>
                <li>
                    <a data-src="track path" href="#">title</a>
                </li>
                <li>
                    <a data-src="track path" href="#">title</a>
                </li>
                <li>
                    <a data-src="track path" href="#">title</a>
                </li>
            </ul>
        </div>

I need to get the song path from the XML document and add it to the "data-src" attribute, and get the song title and display that as an anchor link. 我需要从XML文档获取歌曲路径,并将其添加到“ data-src”属性,并获取歌曲标题并将其显示为锚链接。

I have about 6 tracks going into the playlist so I need to loop through each item in the XML and output that data in its own list item. 我大约有6首曲目进入播放列表,因此我需要遍历XML中的每个项目并在其自己的列表项目中输出该数据。

PHP has a built in XML parser. PHP具有内置的XML解析器。

http://php.net/manual/en/book.xml.php http://php.net/manual/zh/book.xml.php

EDIT: This lib might work a bit easier if your structure is known ahead of time... http://www.php.net/manual/en/simplexml.examples-basic.php 编辑:如果您的结构提前知道,此库可能会更容易工作... http://www.php.net/manual/zh/simplexml.examples-basic.php

Using that, as well as a CURL or standard file_get_contents() call, you should be able to have the server retrieve the XML, parse it into a tree-structure, and iterate through the results to generate the HTML for display. 使用该代码以及CURL或标准的file_get_contents()调用,您应该能够使服务器检索XML,将其解析为树结构,并遍历结果以生成显示的HTML。

<?php
$playlistXML = file_get_contents('http://whatever.cms.com/playlist.xml');
$playlist = new SimpleXMLElement($playlistXML);
foreach($playlist->item as $song) {  ?>
   <a href="<?= $song->path; ?>"><?= $song->title.' - '.$song->artist; ?> </a>
<?php } ?>

I'd vote for SimpleXML . 我会投票支持SimpleXML

Putting it altogether, you'll load the XML from your server, parse it using SimpleXML, and then iterate over each song in the list to template list items using the provided title and artist. 综上所述,您将从服务器上加载XML,使用SimpleXML对其进行解析,然后使用提供的标题和艺术家遍历列表中的每首歌曲以将列表项模板化。

<?php
/* first load the XML and create the containing div */
    $playlistRawXML = file_get_contents('http://example.com/path/to/playlist.xml');

    try {
       $playlist = new SimpleXMLElement($playlistRawXML);
    } catch (Exception $e) {
       /* if SimpleXML can't parse the file, it'll throw an exception */
       echo "XML parsing error";
       var_dump($e);
       exit;
    }
?>
<div id="player-holder">
    <audio preload></audio>
    <ul>

<?php
    /* then, for each song in the playlist, render a list item: */

    foreach($playlist->item as $song) {  
        echo '<li><a data-src="' . $song->path . '" href="#">' . $song->title . ' (' . $song->artist . ')</a></li>';
     }

     /* and then end the list, div, etc.: */
 ?>

  </ul>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM