0

我正在使用 SimpleXMLElement 生成一个 xml 文件。

由于命名空间,我在格式化文件时遇到了一些困难。希望有人可以提供帮助。(获取数据没有问题...只是不确定何时使用 ->addChild、->addAttribute 和其他任何东西)

该文件需要采用这种精确结构,因为另一个应用程序(JW Player)将从中读取:

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

4

1 回答 1

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");
?>

希望能帮助到你。

于 2012-04-05T13:55:57.417 回答