我必须在运行时动态生成一个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>
Run Code Online (Sandbox Code Playgroud)
Iva*_*tov 351
我使用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());
Run Code Online (Sandbox Code Playgroud)
Ein*_*eki 177
要在PHP中创建XML文档,您应该实例化DOMDocument类,创建子节点并将这些节点附加到文档树的正确分支中.
供参考,您可以阅读http://it.php.net/manual/en/book.dom.php
现在我们将快速浏览下面的代码.
这些是基础知识,您可以在一行中创建和附加一个节点(例如,第13个),您可以使用dom api执行许多其他操作.它是由你决定.
<?php
/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$currentTrack = $domtree->createElement("track");
$currentTrack = $xmlRoot->appendChild($currentTrack);
/* you should enclose the following two lines in a cicle */
$currentTrack->appendChild($domtree->createElement('path','song1.mp3'));
$currentTrack->appendChild($domtree->createElement('title','title of song1.mp3'));
$currentTrack->appendChild($domtree->createElement('path','song2.mp3'));
$currentTrack->appendChild($domtree->createElement('title','title of song2.mp3'));
/* get the xml printed */
echo $domtree->saveXML();
?>
Run Code Online (Sandbox Code Playgroud)
编辑:只是另一个提示:使用xmldocument(dom文档one或simplexml one)而不是打印xml的主要优点是xmltree可以使用xpath查询进行搜索
and*_*dyk 28
一个简单的方法是:
<?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>';
?>
Run Code Online (Sandbox Code Playgroud)
将其保存为.php
Dan*_*ndo 15
使用FluidXML,您可以非常轻松地生成XML.
$tracks = fluidxml('xml');
$tracks->times(8, function ($i) {
$this->add([
'track' => [
'path' => "song{$i}.mp3",
'title' => "Track {$i} - Track Title"
]
]);
});
Run Code Online (Sandbox Code Playgroud)
https://github.com/servo-php/fluidxml
希望这段代码可以帮助你。简单易行的解决方案
$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);
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请访问此获取详细信息:https ://www.guru99.com/php-and-xml.html
小智 5
我看到了 DOM 和 SimpleXML 的示例,但没有看到 XMLWriter 的示例。
请记住,根据我所做的测试,DOM 和 SimpleXML 几乎都比 XMLWriter 慢两倍,对于较大的文件,您应该考虑使用后一种。
这是一个完整的工作示例,清晰简单,满足要求,用 XMLWriter 编写(我相信它会对其他用户有所帮助):
// array with the key / value pairs of the information to be added (can be an array with the data fetched from db as well)
$songs = [
'song1.mp3' => 'Track 1 - Track Title',
'song2.mp3' => 'Track 2 - Track Title',
'song3.mp3' => 'Track 3 - Track Title',
'song4.mp3' => 'Track 4 - Track Title',
'song5.mp3' => 'Track 5 - Track Title',
'song6.mp3' => 'Track 6 - Track Title',
'song7.mp3' => 'Track 7 - Track Title',
'song8.mp3' => 'Track 8 - Track Title',
];
$xml = new XMLWriter();
$xml->openURI('songs.xml');
$xml->setIndent(true);
$xml->setIndentString(' ');
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('xml');
foreach($songs as $song => $track){
$xml->startElement('track');
$xml->writeElement('path', $song);
$xml->writeElement('title', $track);
$xml->endElement();
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
unset($xml);
Run Code Online (Sandbox Code Playgroud)