2013-10-07 64 views
1

我試圖使用audiojs插件創建HTML5播放列表。我的播放列表是在外部XML文件,因爲它是由一個定製的CMS管理:從xml文件創建html5播放列表

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

這是我的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> 

我需要從XML文檔中的歌曲和路徑將其添加到「data-src」屬性中,並獲取歌曲標題並將其顯示爲錨鏈接。

我有大約6首曲目進入播放列表,因此我需要遍歷XML中的每個項目並將其輸出到自己的列表項中。

回答

0

PHP有一個內置的XML解析器。

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

編輯:這LIB可能工作更容易一點,如果你的結構是已知的時間提前... http://www.php.net/manual/en/simplexml.examples-basic.php

使用,以及捲曲或標準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 } ?> 
0

我會爲SimpleXML投票。

總之,您將從服務器加載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>