我有这个代码(更大的脚本的一部分):
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,但这并不容易;)
有什么想法吗?
发布于 2011-04-23 00:36:50
不管有趣不好笑,但是试试这个:
<? 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>或者类似的东西。
发布于 2012-12-10 03:31:53
正如我所看到的,用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。
发布于 2011-04-15 09:18:16
这个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。
https://stackoverflow.com/questions/5671405
复制相似问题