如何用xml保存dom

时间:2009-11-16 07:32:29

标签: php xml dom

我用

保存我的dom xml文件
`<?php
if(isset($_POST["song"])&& $_POST['song'] != "") 
    {
        $song = $_POST["song"];
    }
    else {$song=array();} 

$dom = new DOMDocument("1.0");
// display document in browser as plain text 
// for readability purposes

// create root element
$root = $dom->createElement("playlist");
$dom->appendChild($root);
$root->setAttribute('version', "1");
$root->setAttribute('xmlns', "http://xspf.org/ns/0/");
$rootnext = $dom->createElement("trackList");
$root->appendChild($rootnext);
foreach ($song as $counter) {
    $tokens = ",";
    $tokenized = strtok($counter, $tokens);
// create child element

$song = $dom->createElement("track");
$rootnext->appendChild($song);
$song1 = $dom->createElement("creator");
$song->appendChild($song1);
$text = $dom->createTextNode("www.indiantags.com");
$song1->appendChild($text); 
$song1 = $dom->createElement("title");
$song->appendChild($song1);
// create text node
$text = $dom->createTextNode($tokenized);
$song1->appendChild($text); 
$tokenized = strtok($tokens);
$song1 = $dom->createElement("location");
$song->appendChild($song1);
$text = $dom->createTextNode($tokenized);
$song1->appendChild($text); 

}

// save 
$dom->save("playlist.xml");

?>
<object data="42-mp3player.swf?autostart=true&playlist=playlist.xml" type="application/x-shockwave-flash" width="400" height="300"><param name="movie" value="42-mp3player.swf?autostart=true&playlist=playlist.xml"/></object>

`

但我需要的是我想用动态名称保存这个带有microtime函数的playlist.xml文件,或者使用sessionid名称。任何人都希望对它有所了解吗?

谢谢

2 个答案:

答案 0 :(得分:0)

IXMLDOMDocument2接口有一个save方法。检查this

答案 1 :(得分:0)

要使用动态名称保存,您可以在PHP中执行以下操作:

//Set dynamic name - used microtime in this example but you could change this
//to another dynamic naming scheme
$dynamicPlaylistName = microtime();

//Save XML with dynamic name
$dom->save($dynamicPlaylistName.'.xml');

上面的代码将dynamicPlaylistName设置为在该行上等号后面放置的任何内容,然后使用dynamicPlaylistName的值保存xml作为其文件名,并附加“.xml”。

如果您还希望使用缩进和嵌套格式化XML文件,请参阅我对this question的回答。请注意,您可能只需要在保存XML之前在某处添加以下两行,以便在您的情况下进行格式化:

$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;