gpt4 book ai didi

php - 如何使用 PHP 动态生成 XML 文件?

转载 作者:IT老高 更新时间:2023-10-28 11:39:19 25 4
gpt4 key购买 nike

我必须在运行时动态生成一个 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>

最佳答案

我会使用 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() 也可以将文件名作为参数保存到该文件中

关于php - 如何使用 PHP 动态生成 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/486757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com