php 如何使用PHP动态生成XML文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/486757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate XML file dynamically using PHP?
提问by musicking123
I have to generate a xml file dynamically at runtime. Please help me in generating the below XML file dynamically using PHP.
我必须在运行时动态生成一个 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>
回答by Ivan Krechetov
I'd use SimpleXMLElement.
我会使用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());
回答by Eineki
To create an XMLdocument in PHP you should instance a DOMDocument class, create child nodes and append these nodes in the correct branch of the document tree.
要在 PHP 中创建 XML 文档,您应该实例化一个 DOMDocument 类,创建子节点并将这些节点附加到文档树的正确分支中。
For reference you can read http://it.php.net/manual/en/book.dom.php
作为参考,您可以阅读http://it.php.net/manual/en/book.dom.php
Now we will take a quick tour of the code below.
现在我们将快速浏览下面的代码。
- at line 2 we create an empty xml document (just specify xml version (1.0) and encoding (utf8))
- now we need to populate the xml tree:
- We have to create an xmlnode (line 5)
- and we have to append this in the correct position. We are creating the root so we append this directly to the domdocument.
- Note create element append the element to the node and return the node inserted, we save this reference to append the track nodes to the root node (incidentally called xml).
- 在第 2 行,我们创建了一个空的 xml 文档(只需指定 xml 版本(1.0)和编码(utf8))
- 现在我们需要填充 xml 树:
- 我们必须创建一个 xmlnode(第 5 行)
- 我们必须将其附加到正确的位置。我们正在创建根,因此我们将它直接附加到 domdocument。
- 注意create element 将元素追加到节点并返回插入的节点,我们保存此引用以将跟踪节点追加到根节点(顺便称为xml)。
These are the basics, you can create and append a node in just a line (13th, for example), you can do a lot of other things with the dom api. It is up to you.
这些是基础知识,您可以在一行中创建和附加一个节点(例如第 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();
?>
Edit: Just one other hint: The main advantage of using an xmldocument (the dom document one or the simplexml one) instead of printing the xml,is that the xmltree is searchable with xpath query
编辑:还有一个提示:使用 xmldocument(dom 文档或 simplexml 文档)而不是打印 xml 的主要优点是可以使用 xpath 查询搜索 xmltree
回答by andyk
An easily broken way to do this is :
一个容易打破的方法是:
<?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>';
?>
save it as .php
将其另存为 .php
回答by Daniele Orlando
With FluidXMLyou can generate your XML very easly.
使用FluidXML,您可以非常轻松地生成 XML。
$tracks = fluidxml('xml');
$tracks->times(8, function ($i) {
$this->add([
'track' => [
'path' => "song{$i}.mp3",
'title' => "Track {$i} - Track Title"
]
]);
});
回答by Cruachan
Take a look at the Tiny But Strongtemplating system. It's generally used for templating HTML but there's an extension that works with XML files. I use this extensively for creating reports where I can have one code file and two template files - htm and xml - and the user can then choose whether to send a report to screen or spreadsheet.
看看Tiny But Strong模板系统。它通常用于模板化 HTML,但有一个适用于 XML 文件的扩展。我广泛使用它来创建报告,其中我可以拥有一个代码文件和两个模板文件 - htm 和 xml - 然后用户可以选择是将报告发送到屏幕还是电子表格。
Another advantage is you don't have to code the xml from scratch, in some cases I've been wanting to export very large complex spreadsheets, and instead of having to code all the export all that is required is to save an existing spreadsheet in xml and substitute in code tags where data output is required. It's a quick and a very efficient way to work.
另一个优点是您不必从头开始编写 xml,在某些情况下,我一直想导出非常大的复杂电子表格,而不必编写所有导出代码,只需将现有电子表格保存在xml 并替换需要数据输出的代码标记。这是一种快速且非常有效的工作方式。
回答by pratik
$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");
Its simple just connect with your database it will create test.xml file in your project folder
它很简单,只需连接您的数据库,它就会在您的项目文件夹中创建 test.xml 文件

