首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XML文件中的PHP (或PHP文件作为XML文件)

XML文件中的PHP (或PHP文件作为XML文件)
EN

Stack Overflow用户
提问于 2011-04-15 09:06:38
回答 3查看 8K关注 0票数 0

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

代码语言:javascript
复制
flashvars.xmlSource = "datasource.xml";   

datasource.xml看起来像这样:

代码语言:javascript
复制
<?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,但这并不容易;)

有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-23 00:36:50

不管有趣不好笑,但是试试这个:

  1. 将您的文件扩展名保留为“xml”
  2. 您写入的位置(...)写入<? PHP CODE HERE ?>

所以要像处理某个html文件一样处理它。我的意思是:

代码语言:javascript
复制
<?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>

另请注意

这一行

代码语言:javascript
复制
<Source="address" Title="title"></Source>

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

代码语言:javascript
复制
<Source name="address" Title="title"></Source>

或者类似的东西。

票数 2
EN

Stack Overflow用户

发布于 2012-12-10 03:31:53

正如我所看到的,用php生成xml文件可以用这种方式来完成-例如,您将创建文件datasource.xml,它将不是一个静态xml文件,而是包含php代码的xml,这些内容包括

代码语言:javascript
复制
<?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主机配置文件中添加一些指令。在我的例子中,我添加了

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2011-04-15 09:18:16

这个XML是否需要存储在服务器上的某个位置,或者您可以只向它传递一个格式类似于您提到的XML的字符串。您可以编写一个函数,根据输入生成您要查找的XML并返回它,然后像这样调用它

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5671405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档