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>
6 个解决方案
#1
315
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());
#2
162
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中创建XMLdocument,您应该实例化一个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))
- 在第2行,我们创建一个空的xml文档(只需指定xml版本(1.0)和编码(utf8)))
- now we need to populate the xml tree:
- We have to create an xmlnode (line 5)
- 我们必须创建一个xmlnode(第5行)
- and we have to append this in the correct position. We are creating the root so we append this directly to the domdocument.
- 我们必须在正确的位置加上这个。我们正在创建根节点,因此我们将它直接附加到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).
- 注意创建元素将元素附加到节点并返回已插入的节点,我们将此引用保存为将跟踪节点附加到根节点(顺便称为xml)。
- 现在我们需要填充xml树:我们必须创建一个xmlnode(第5行),并且必须将其附加到正确的位置。我们正在创建根节点,因此我们将它直接附加到domdocument。注意创建元素将元素附加到节点并返回已插入的节点,我们将此引用保存为将跟踪节点附加到根节点(顺便称为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 document one或simplexml one)而不是打印xml的主要优点是可以用xpath查询xmltree
#3
26
an easy 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
#4
13
With FluidXML you 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"
]
]);
});
https://github.com/servo-php/fluidxml
https://github.com/servo-php/fluidxml
#5
4
Take a look at the Tiny But Strong templating 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.
看看这个微小但强大的模板系统。它通常用于模板化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和代入代码标记是必需的。这是一种快速有效的工作方式。
#6
-9
$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
它简单的连接你的数据库,它将创建测试。项目文件夹中的xml文件。
#1
315
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());
#2
162
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中创建XMLdocument,您应该实例化一个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))
- 在第2行,我们创建一个空的xml文档(只需指定xml版本(1.0)和编码(utf8)))
- now we need to populate the xml tree:
- We have to create an xmlnode (line 5)
- 我们必须创建一个xmlnode(第5行)
- and we have to append this in the correct position. We are creating the root so we append this directly to the domdocument.
- 我们必须在正确的位置加上这个。我们正在创建根节点,因此我们将它直接附加到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).
- 注意创建元素将元素附加到节点并返回已插入的节点,我们将此引用保存为将跟踪节点附加到根节点(顺便称为xml)。
- 现在我们需要填充xml树:我们必须创建一个xmlnode(第5行),并且必须将其附加到正确的位置。我们正在创建根节点,因此我们将它直接附加到domdocument。注意创建元素将元素附加到节点并返回已插入的节点,我们将此引用保存为将跟踪节点附加到根节点(顺便称为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 document one或simplexml one)而不是打印xml的主要优点是可以用xpath查询xmltree
#3
26
an easy 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
#4
13
With FluidXML you 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"
]
]);
});
https://github.com/servo-php/fluidxml
https://github.com/servo-php/fluidxml
#5
4
Take a look at the Tiny But Strong templating 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.
看看这个微小但强大的模板系统。它通常用于模板化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和代入代码标记是必需的。这是一种快速有效的工作方式。
#6
-9
$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
它简单的连接你的数据库,它将创建测试。项目文件夹中的xml文件。