简体   繁体   中英

Create a html5 playlist from an xml file

I am trying to create a HTML5 playlist using the audiojs plugin. My playlist is in an external XML file as it is managed by a custom 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:

        <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.

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.

PHP has a built in XML parser.

http://php.net/manual/en/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

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.

<?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 .

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.

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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