1

audiojsプラグインを使用して HTML5 プレイリストを作成しようとしています。カスタム CMS によって管理されているため、プレイリストは外部 XML ファイルにあります。

<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 の各アイテムをループして、そのデータを独自のリスト アイテムに出力する必要があります。

4

2 に答える 2

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>
于 2013-10-07T21:43:43.067 に答える
0

PHP には XML パーサーが組み込まれています。

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

編集:構造が事前にわかっている場合、このライブラリは少し簡単に機能する可能性があります... http://www.php.net/manual/en/simplexml.examples-basic.php

それを 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 } ?>
于 2013-10-07T21:21:05.007 に答える