XML 文件中的 PHP(或 XML 文件形式的 PHP 文件)
我有这个代码(更大脚本的一部分):
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 但这并不那么容易;)
有什么想法吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不管有趣与否,但尝试一下这个:
所以处理它就像它是一些 html 文件一样。我的意思是:
还要注意
这一行
可能是错误的(您为标记名分配了一些值),请尝试
或类似的操作。
Funny or not, but try this one:
<? PHP CODE HERE ?>
So handle it as if it would be some html file. What I mean is:
Also note
this line
might be wrong (you assigned some value to the tagname), try
or something like that.
正如我所看到的,用 php 生成 xml 文件可以通过这种方式完成 - 例如,您将创建文件 datasource.xml ,它不是静态 xml 文件,而是带有 php 代码的 xml,其中包含类似的内容,
以便允许 php 执行 php XML 文件中的代码我们需要向 apache 主机配置文件添加一些指令。就我而言,我添加了
在我的虚拟主机配置文件中,或者如果您的主机配置中允许覆盖此参数,您可以将此命令放在目录中的 .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
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
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.
此 XML 是否需要成为服务器上某处的存储文件,或者您可以只向其传递一个格式类似于您提到的 XML 的字符串。您可以编写一个函数,根据输入生成您正在查找的 XML 并返回它,然后调用该函数,例如
如果您需要实际生成并存储格式良好的 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
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