我必须在运行时动态生成一个xml文件。请帮助我用PHP动态生成下面的XML文件。

<?xml version="1.0" encoding="UTF-8"?>
<xml>
 <track>
     <path>song1.mp3</path>
     <title>Track 1 - Track Title</title>
 </track>
 <track>
     <path>song2.mp3</path>
     <title>Track 2 - Track Title</title>
 </track>
 <track>
     <path>song3.mp3</path>
     <title>Track 3 - Track Title</title>
 </track>
 <track>
     <path>song4.mp3</path>
     <title>Track 4 - Track Title</title>
 </track>
 <track>
     <path>song5.mp3</path>
     <title>Track 5 - Track Title</title>
 </track>
 <track>
     <path>song6.mp3</path>
     <title>Track 6 - Track Title</title>
 </track>
 <track>
     <path>song7.mp3</path>
     <title>Track 7 - Track Title</title>
 </track>
 <track>
     <path>song8.mp3</path>
     <title>Track 8 - Track Title</title>
 </track>


当前回答

使用FluidXML,您可以非常容易地生成XML。

$tracks = fluidxml('xml');

$tracks->times(8, function ($i) {
    $this->add([
        'track' => [
            'path'  => "song{$i}.mp3",
            'title' => "Track {$i} - Track Title"
        ]
    ]);

});

https://github.com/servo-php/fluidxml

其他回答

一种容易被破坏的方法是:

<?php
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

echo '<xml>';

// echo some dynamically generated content here
/*
<track>
    <path>song_path</path>
    <title>track_number - track_title</title>
</track>
*/

echo '</xml>';

?>

将其保存为.php

希望这段代码可以帮助你。简单易行的解决方案

$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$xml_file_name = './movies_list.xml'; //You can give your path to save file.
$root = $dom->createElement('Movies');
    $movie_node = $dom->createElement('movie');
    $attr_movie_id = new DOMAttr('movie_id', '5467');
    $movie_node->setAttributeNode($attr_movie_id);
        
        $child_node_title = $dom->createElement('Title', 'The Campaign');
        $movie_node->appendChild($child_node_title);
        
        $child_node_year = $dom->createElement('Year', 2012);
        $movie_node->appendChild($child_node_year);
        
        $child_node_genre = $dom->createElement('Genre', 'The Campaign');
        $movie_node->appendChild($child_node_genre);
        
        $child_node_ratings = $dom->createElement('Ratings', 6.2);
        $movie_node->appendChild($child_node_ratings);
    $root->appendChild($movie_node);
$dom->appendChild($root);
$dom->save($xml_file_name);

欲了解更多信息,请访问https://www.guru99.com/php-and-xml.html以获取详细信息

我将使用SimpleXMLElement。

<?php

$xml = new SimpleXMLElement('<xml/>');

for ($i = 1; $i <= 8; ++$i) {
    $track = $xml->addChild('track');
    $track->addChild('path', "song$i.mp3");
    $track->addChild('title', "Track $i - Track Title");
}

Header('Content-type: text/xml');
print($xml->asXML());

?>

$xml->asXML()也可以将文件名作为参数保存到该文件中

使用FluidXML,您可以非常容易地生成XML。

$tracks = fluidxml('xml');

$tracks->times(8, function ($i) {
    $this->add([
        'track' => [
            'path'  => "song{$i}.mp3",
            'title' => "Track {$i} - Track Title"
        ]
    ]);

});

https://github.com/servo-php/fluidxml

$query=mysql_query("select * from tablename")or die(mysql_error()); 
$xml="<libraray>\n\t\t";
while($data=mysql_fetch_array($query))
{

    $xml .="<mail_address>\n\t\t";
    $xml .= "<id>".$data['id']."</id>\n\t\t";
    $xml .= "<email>".$data['email_address']."</email>\n\t\t";
    $xml .= "<verify_code>".$data['verify']."</verify_code>\n\t\t";
    $xml .= "<status>".$data['status']."</status>\n\t\t";
    $xml.="</mail_address>\n\t";
}
$xml.="</libraray>\n\r";
$xmlobj=new SimpleXMLElement($xml);
$xmlobj->asXML("text.xml");

它很简单,只要连接到你的数据库,它会在你的项目文件夹中创建test.xml文件