我有这段代码(更大的脚本的一部分):
flashvars.xmlSource = "datasource.xml";
datasource.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source="address" Title="title"></Source>
<Description><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Description>
(...)
</Contents>
</Object>
我想使用foreach循环动态生成datasource.xml。
我刚刚将文件扩展名更改为.php,但这并不容易;)
有什么想法吗?
答案 0 :(得分:2)
有趣与否,但试试这个:
<? PHP CODE HERE ?>
所以处理它好像是一些html文件。我的意思是:
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source="address" Title="title"></Source>
<Description><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Description>
<? create php loop here ?>
</Contents>
</Object>
另请注意
这一行
<Source="address" Title="title"></Source>
可能是错误的(您为标记名指定了一些值),请尝试
<Source name="address" Title="title"></Source>
或类似的东西。
答案 1 :(得分:2)
我看到用php生成xml文件可以用这种方式完成 - 比如你将创建文件datasource.xml,它不是一个静态的xml文件,而是包含内容的php代码的xml
<?php //php code to generate any xml code as Text
// it can be whatever you need to generate
// for example
$content="<h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p>";
$output="<Description>".$content."</Description>";
header('Content-type: application/xml');// this is most important php command which says that all output text is XML it must be called before any line of xml will be printed.
// So you need at first generate XML as text then call this command and echo contents of your xml file.
?>
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source name="address" Title="title"></Source>
<? echo $output; ?>
</Contents>
</Object>
为了让php在XML文件中执行php代码,我们需要在apache主机配置文件中添加一些指令。在我的情况下,我添加了
<IfModule mod_php5.c>
<FilesMatch "\.xml$">
SetHandler application/x-httpd-php
</FilesMatch>
在我的虚拟主机配置文件中,或者如果在主机配置中允许覆盖此参数,则可以将此命令放在目录中的.htaccess文件中。 关于xml-以确保没关系,您可以使用http://validator.w3.org/或http://www.w3schools.com/xml/xml_validator.asp来验证脚本生成的xml。
答案 2 :(得分:0)
这个XML是否需要在服务器上的某个位置存储文件,或者您可以只传递一个格式与您提到的XML类似的字符串。您可以编写一个函数来生成您正在寻找的XML并根据输入返回它,然后调用它
function generateXML($input){
$xml = '<?xml version="1.0" encoding="utf-8"?><Object><Contents>
<Source="address" Title="title"></Source><Whateverelse>' . $input;
$xml .= '</Whateverelse></Contents></Object>';
return $xml;}
flashvars.xmlSource = generateXML("This is whatever else");
如果您需要实际生成和存储格式良好的XML文档,或者您的XML相当复杂并且需要生成对象而不是仅使用字符串,则可以使用其中一个PHP库来执行此操作http://php.net/manual/en/book.simplexml.php