XML 文件中的 PHP(或 XML 文件形式的 PHP 文件)

发布于 2024-11-02 03:48:38 字数 993 浏览 1 评论 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 但这并不那么容易;)

有什么想法吗?

I have this code (part of bigger script):

flashvars.xmlSource = "datasource.xml";   

datasource.xml looks like:

<?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> 

I want to generate datasource.xml dynamically using foreach loop.

I've just changed the file extension to .php but this is not that easy ;)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天荒地未老 2024-11-09 03:48:38

不管有趣与否,但尝试一下这个:

  1. 将文件扩展名保留为“xml”,
  2. 在其中写入 (...) write

所以处理它就像它是一些 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>

或类似的操作。

Funny or not, but try this one:

  1. leave your file extension to be "xml"
  2. where you wrote (...) write <? PHP CODE HERE ?>

So handle it as if it would be some html file. What I mean is:

<?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>

Also note

this line

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

might be wrong (you assigned some value to the tagname), try

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

or something like that.

友欢 2024-11-09 03:48:38

正如我所看到的,用 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 执行 php XML 文件中的代码我们需要向 apache 主机配置文件添加一些指令。就我而言,我添加了

<IfModule mod_php5.c>
  <FilesMatch "\.xml$">
        SetHandler application/x-httpd-php
</FilesMatch>

在我的虚拟主机配置文件中,或者如果您的主机配置中允许覆盖此参数,您可以将此命令放在目录中的 .htaccess 文件中。
关于 xml- 为了确保没问题,您可以使用 http://validator.w3.org/ 或 < a href="http://www.w3schools.com/xml/xml_validator.asp" rel="nofollow">http://www.w3schools.com/xml/xml_validator.asp 验证生成的 xml通过你的脚本。

As I see generating xml file with php could be done in this way - for example you'll create file datasource.xml which will be not a static xml file but xml with php code included with contents like

<?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> 

In order to allow php to execute php code inside XML file we need to add some directives to apache host configuration file. In my case I added

<IfModule mod_php5.c>
  <FilesMatch "\.xml$">
        SetHandler application/x-httpd-php
</FilesMatch>

inside my virtual host configuration file, or you can place this command inside .htaccess file in your directory if Override of this param is allowed in your host configuration.
And about xml- to make sure it's ok you can use http://validator.w3.org/ or http://www.w3schools.com/xml/xml_validator.asp to validate xml generated by your script.

自由如风 2024-11-09 03:48:38

此 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

Does this XML need to be a stored file somewhere on the server, or can you just pass it a string formatted like the XML you mentioned. You could write a function that generates the XML you're looking for and returns it, based on input and then call that like

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");

If you need to actually generate and store a well formed XML document, or if your XML is fairly complex and you need to generate an object rather than just using a string, you can utilize one of the PHP libraries to do this like http://php.net/manual/en/book.simplexml.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文