0

我有这段代码(更大脚本的一部分):

flashvars.xmlSource = "datasource.xml";   

datasource.xml 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<Object>
  <Contents>
    <Source="address" Title="title"></Source>
      <Description>&lt;h1&gt;New hot Features&lt;/h1&gt;&lt;p&gt;The all new Piecemaker comes with lots of new features, making it even more slick.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.&lt;/p&gt;</Description>
(...)
  </Contents>
</Object> 

我想使用 foreach 循环动态生成 datasource.xml。

我刚刚将文件扩展名更改为 .php 但这并不容易;)

有任何想法吗?

4

3 回答 3

2

有趣与否,但试试这个:

  1. 将文件扩展名保留为“xml”
  2. 你写的地方(...)写<? PHP CODE HERE ?>

所以处理它就好像它是一些 html 文件一样。我的意思是:

<?xml version="1.0" encoding="utf-8"?>
<Object>
  <Contents>
    <Source="address" Title="title"></Source>
      <Description>&lt;h1&gt;New hot Features&lt;/h1&gt;&lt;p&gt;The all new Piecemaker comes with lots of new features, making it even more slick.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.&lt;/p&gt;</Description>
<? create php loop here  ?>
  </Contents>
</Object>

另请注意

这条线

<Source="address" Title="title"></Source>

可能是错误的(您为标记名分配了一些值),请尝试

<Source name="address" Title="title"></Source>

或类似的东西。

于 2011-04-22T16:36:50.183 回答
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="&lt;h1&gt;New hot Features&lt;/h1&gt;&lt;p&gt;The all new Piecemaker comes with lots of new features, making it even more slick.&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.&lt;/p&gt;";
  $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。

于 2012-12-09T19:31:53.093 回答
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

于 2011-04-15T01:18:16.397 回答