【问题标题】:PHP in XML file (or PHP file as XML one)XML 文件中的 PHP(或作为 XML 文件的 PHP 文件)
【发布时间】:2011-04-15 01:06:37
【问题描述】:

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

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,但这并不容易;)

有什么想法吗?

【问题讨论】:

    标签: php xml flashvars


    【解决方案1】:

    有趣与否,但试试这个:

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

    所以把它当作一些 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>
    

    或类似的东西。

    【讨论】:

      【解决方案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。

      【讨论】:

        【解决方案3】:

        此 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-20
          • 2015-10-18
          • 1970-01-01
          • 2014-07-18
          • 2014-03-27
          相关资源
          最近更新 更多