2011-01-08 9 views
1

私のフラッシュサイトのコンテンツを編集するにはxmlファイルのヘルプが必要です。例えば、 "name"などのテキストエリアに挿入するたびにxmlで新しいアクティビティを作成します。「水泳」から「名前」に変更。 xmlでコンテンツを変更するには、「水泳」を削除し、「名前」を挿入する必要があることが分かりました。php textareaを使用したxmlファイルのテストの更新

まず、テキストエリアにコンテンツを挿入する - >「更新」を押すと保存する - >古いアクティビティをXMLファイルから削除し、新しいコンテンツをXMLファイルに挿入する

<?php 
    header('Location:index.php'); 
    $xmldoc = new DOMDocument(); 
    $xmldoc->load('sample.xml'); 

    $newAct = $_POST['activity']; 

    $root = $xmldoc->firstChild; 

    $newElement = $xmldoc->createElement('activity'); 
    $root->appendChild($newElement); 
    $newText = $xmldoc->createTextNode($newAct); 
    $newElement->appendChild($newText); 

    $xmldoc->save('sample.xml'); 
?> 

insert.php

sample.xmlに

<?xml version="1.0"?> 
<list> 
    <activity>swimming</activity> 
</list> 

のindex.php

<html> 
<head><title>test</title></head> 
</head> 

<body> 


<table width="100" border="1"> 
    <tr> 
    <td><?php 
    $xmldoc = new DOMDocument(); 
    $xmldoc->load("sample.xml", LIBXML_NOBLANKS); 

    $activities = $xmldoc->firstChild->firstChild; 
    if($activities!=null){ 
     while($activities!=null){ 
      echo $activities->textContent.""; 
      $activities = $activities->nextSibling; 
     } 
    } 
?></td> 
    </tr> 
    <tr> 
    <td><form name="input" action="insert.php" method="post"> 

     <textarea name="activity" cols="70" rows="10"><?php 
    $xmldoc = new DOMDocument(); 
    $xmldoc->load("sample.xml", LIBXML_NOBLANKS); 

    $activities = $xmldoc->firstChild->firstChild; 
    if($activities!=null){ 
     while($activities!=null){ 
      echo $activities->textContent.""; 
      $activities = $activities->nextSibling; 
     } 
    } 
?></textarea> 
    </td> 
    </tr> 
    <tr> 
    <td align="right"><input type="submit" value="Update"/> 
    <input name="reset" type="reset" id="reset" value="Reset"> 
</form></td> 
    </tr> 
</table> 

</body> 
</html> 

あなたの助けありがとうございました!!!

+0

あなたはHTMLエスケープではありません。 'echo $ activities-> textContent。" ";'いくつかのregexp messよりむしろDOMDocumentをXMLのために使用するためのOTOHの名声:) – Kornel

答えて

1

あなたのWebページ上のコンテンツを追加または削除する必要がある場合、私はちょうどすべてのおかげだ

新しいinsert.php

<?php 
$xmldoc = new DOMDocument; 
$xmldoc->load('sample.xml'); 
foreach ($xmldoc->getElementsByTagName('activity') as $activity) 
{ 
    $activity->parentNode->removeChild($activity); 
} 
$xmldoc->save('sample.xml'); 

?> 

<?php 

    $xmldoc = new DOMDocument(); 
    $xmldoc->load('sample.xml'); 

    $newAct = $_POST['activity']; 

    $root = $xmldoc->firstChild; 

    $newElement = $xmldoc->createElement('activity'); 
    $root->appendChild($newElement); 
    $newText = $xmldoc->createTextNode($newAct); 
    $newElement->appendChild($newText); 

    $xmldoc->save('sample.xml'); 

?> 

insert.php除いて、上からすべてコピーし、自分で管理している大丈夫