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 に変更しましたが、これは簡単ではありません ;)

何か案は?

4

3 に答える 3

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>

またはそのようなもの。

于 2011-04-22T16:36:50.183 に答える
2

私が見るように、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 を検証できることを確認してください。

于 2012-12-09T19:31:53.093 に答える
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 ライブラリの 1 つを利用してhttp:/ /php.net/manual/en/book.simplexml.php

于 2011-04-15T01:18:16.397 に答える