php动态生成一个xml文件供swf调用

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="240" height="200" id="dewplayer" type="application/x-shockwave-flash">
  <param name="allowScriptAccess" value="sameDomain" />
  <param name="movie" value="dewplayer-playlist.swf?xml=xml.php&showtime=true" />
  <param name="quality" value="high" />
  <param name="bgcolor" value="#ffffff" />
  <param name="wmode" value="transparent" />
  <embed src="dewplayer-playlist.swf?xml=xml.php&&showtime=true" wmode="transparent" quality="high" bgcolor="#ffffff" width="240" height="200" name="dewplayer" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
  </object>

xml.php

 

<?php
header('Content-type: application/xml');
//数据可以从数据库是读取
$data_array = array(
 array("location"=>"mp3/moldau.mp3","creator"=>"Bedrich Smetana","album"=>"","title"=>"","annotation"=>"","duration"=>"","image"=>"","info"=>"","link"=>""),
 array("location"=>"mp3/moldau.mp3","creator"=>"Cesaria Evora","album"=>"","title"=>"","annotation"=>"","duration"=>"","image"=>"","info"=>"","link"=>""),
 array("location"=>"mp3/moldau.mp3","creator"=>"Russian Red Army Choir","album"=>"","title"=>"","annotation"=>"","duration"=>"","image"=>"","info"=>"","link"=>"")
);
//  属性数组
/*$attribute_array = array(

    'title' => array(

    'size' => 1

    )

);
*/
 
//  创建一个XML文档并设置XML版本和编码。。
$dom=new DomDocument('1.0', 'utf-8');

//  创建根节点

$playlist = $dom->createElement('playlist');

$dom->appendchild($playlist);
/////////////
$title=$dom->createElement('title');
$playlist->appendchild($title);
$text = $dom->createTextNode('Ounage Playlist');
$title->appendchild($text);
/////////////////////

//trackList
$trackList=$dom->createElement('trackList');
$playlist->appendchild($trackList);

//
foreach ($data_array as $data) {

    $item = $dom->createElement('track');

    $trackList->appendchild($item);
 
    create_item($dom, $item, $data, $attribute_array);
}

echo $dom->saveXML();

function create_item($dom, $item, $data, $attribute) {

    if (is_array($data)) {

        foreach ($data as $key => $val) {

            //  创建元素

            $$key = $dom->createElement($key);

            $item->appendchild($$key);


            //  创建元素值

            $text = $dom->createTextNode($val);

            $$key->appendchild($text);


            if (isset($attribute[$key])) {

            //  如果此字段存在相关属性需要设置

                foreach ($attribute[$key] as $akey => $row) {

                    //  创建属性节点

                    $$akey = $dom->createAttribute($akey);

                    $$key->appendchild($$akey);


                    // 创建属性值节点

                    $aval = $dom->createTextNode($row);

                    $$akey->appendChild($aval);

                }

            }   //  end if

        }

    }   //  end if

}   //  end function

?>

你可能感兴趣的:(xml)