2012-04-05 103 views
0

我正在使用SimpleXMLElement生成一個xml文件。用命名空間輸出xml文件

由於命名空間,我在格式化文件時遇到了一些困難。希望有人能幫助。 (沒有問題獲取數據...只是不確定何時使用 - > addChild, - > addAttribute和其他)

此文件需要在這個確切的結構,因爲另一個應用程序(JW播放器)將讀取它:

//xml structure 
<?xml version="1.0"?> 
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://developer.longtailvideo.com/trac/" > 
<channel> 
<title>MRSS Playlist Playlist</title> 
... 
<item> 
<title>Cardio routine with handweights</title> 
<media:content url="videos/set2_first_5min.flv" /> 
<media:thumbnail url="thumbs/set2_first.png" /> 
<description>side jumps, arm curls, leg squats, arm lifts</description> 
<jwplayer:duration>300</jwplayer:duration> 
<jwplayer:start>5</jwplayer:start> 
</item> 
... 
</channel> 
</rss> 
//export xml file 
$xml->asXML("devplaylist.xml"); 

任何幫助,將不勝感激。 謝謝, DK

+0

pl。看到我的迴應。 – 2012-04-05 14:14:04

回答

0

試試這個:

<?php 
$xml = simplexml_load_string('<?xml version="1.0"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://developer.longtailvideo.com/trac/"></rss>'); 
$channel = $xml->addChild('channel'); 

$channel->addChild('title', 'TITLE_HERE'); 
$channel->addChild('link', 'LINK_HERE'); 
$channel->addChild('description', 'DESCRIPTION_HERE'); 
$channel->addChild('language', 'LANGUAGE_HERE'); 
$channel->addChild('copyright', 'COPYRIGHT_HERE'); 
// rest of your nodes 

$i=0; 
while(++$i < 2) { // will run only once, only for demo purpose... 
    $item = $channel->addChild('item'); 

    $item->addChild('title', 'Cardio routine with handweights'); 
    $mediaContent = $item->addChild('content', '', 'http://search.yahoo.com/mrss/'); 
    $mediaContent->addAttribute('url', 'videos/set2_first_5min.flv'); 
    $mediaThumbnail = $item->addChild('thumbnail', '', 'http://search.yahoo.com/mrss/'); 
    $mediaThumbnail->addAttribute('url', 'thumbs/set2_first.png'); 
    $item->addChild('description', 'side jumps, arm curls, leg squats, arm lifts'); 
    $item->addChild('duration', 300, 'http://developer.longtailvideo.com/trac/'); 
    $item->addChild('start', 5, 'http://developer.longtailvideo.com/trac/'); 
} 

//export xml file 
$xml->asXML("devplaylist.xml"); 
?> 

希望它能幫助。

+0

對!感謝您幫助我。它工作完美。 – Derek 2012-04-05 19:38:32