PHP

2011-04-15 18 views
0

我有這樣的代碼(大腳本的一部分):PHP

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> 

我想生成datasource.xml動態使用foreach循環。

我剛剛更改的文件擴展名.PHP但是這並不是那麼容易;)

任何想法?

回答

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> 

什麼像那樣。

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

2

當我看到生成的XML文件用PHP可以這樣做 - 比如你將創建這將不是一個靜態的XML文件,但XML與PHP代碼文件datasource.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。