2012-10-01 71 views
1

我有我打印我的所有XML這樣的文本區域:將textarea的XML保存到xml文件中?

<form method="post" action=""> 
<textarea id="codeTextarea" name="thisxml" cols="100" rows="36"> 
<?php 
$xml = new DOMDocument(); 
$xml->formatOutput = true; 
$xml->preserveWhiteSpace = false; 
$xml->loadXML('<?xml version="1.0" encoding="ISO-8859-1"?> 
<data> 
    <game id="103478"> 
    <opponent>Peter</opponent> 
    <oppid>4</oppid> 
    <lastdraw>0</lastdraw> 
    </game> 
    <game id="103479"> 
    <opponent>Peter</opponent> 
    <oppid>4</oppid> 
    <lastdraw>2</lastdraw> 
    </game> 
    <game id="103483"> 
    <opponent>James</opponent> 
    <oppid>47</oppid> 
    <lastdraw>2</lastdraw> 
    </game> 
</data>'); 

echo htmlspecialchars($xml->saveXML()); 
?> 
</textarea> 

然後,我就提出要創建/更新使用新的XML文件,但所有我的新的XML文檔中得到的是這樣的:

<?xml version="1.0"?> 

我儘量節省這樣的XML與PHP:

$myFile = 'TEST.xml'; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
$stringData = htmlspecialchars($_POST['thisxml']); 
fwrite($fh, $stringData); 
fclose($fh); 

有人可以告訴我什麼,我做錯了什麼?

在此先感謝;-)

+0

確定,要保存它與編碼'htmlspecialchars'而不是簡單的'$ _POST [thisxml]'字符串(我的意思是行'$ stringData = htmlspecialchars($ _ POST ['thisxml']);') – MiDo

回答

3

使用htmlspecialchars($_POST['thisxml'])會使您XML invalid的回報類似

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt; 
&lt;data&gt; 
    &lt;game id=&quot;103478&quot;&gt; 
    &lt;opponent&gt;Peter&lt;/opponent&gt; 
    &lt;oppid&gt;4&lt;/oppid&gt; 
    &lt;lastdraw&gt;0&lt;/lastdraw&gt; 
    &lt;/game&gt; 
    &lt;game id=&quot;103479&quot;&gt; 
    &lt;opponent&gt;Peter&lt;/opponent&gt; 
    &lt;oppid&gt;4&lt;/oppid&gt; 
    &lt;lastdraw&gt;2&lt;/lastdraw&gt; 
    &lt;/game&gt; 
    &lt;game id=&quot;103483&quot;&gt; 
    &lt;opponent&gt;James&lt;/opponent&gt; 
    &lt;oppid&gt;47&lt;/oppid&gt; 
    &lt;lastdraw&gt;2&lt;/lastdraw&gt; 
    &lt;/game&gt; 
&lt;/data&gt; 

只要使用file_put_contents它結合了fopen , fwrite , fclose

file_put_contents('TEST.xml', $_POST['thisxml']); 
+1

超級,但是當它這樣做時,它將「outcomment all」 \如下所示:<?xml version = \「1.0 \」encoding = \「ISO-8859-1 \」?> Mansa

+0

Whe點擊鏈接[php.net/manual/en/security.magicquotes.php]我沒有頁面?我在哪裏關掉它? – Mansa

+0

對不起,我犯了一個錯字。這是我的評論:這可能是由於[「魔術引號」](http://php.net/manual/en/security.magicquotes.php),它會自動插入一個反斜槓來轉義'',''''反斜槓和'NULL'字符您應該關閉該功能,因爲它已被棄用 – Andrew

0

您可以保存功能直接使用DOMDocument::save的XML文件:

$xml = new DOMDocument(); 
$xml->formatOutput = true; 
$xml->preserveWhiteSpace = false; 

if ($xml->loadXML($_POST['thisxml']) === FALSE) 
{ 
    die("The submitted XML is invalid"); 
} 

if ($xml->save('TEST.xml') === FALSE) 
{ 
    die("Can't save file"); 
} 
+0

當我這樣做時,我只在文檔中得到<?xml version =「1.0」?>? – Mansa

+0

我認爲它與Magic Quotes有關。你可以閱讀關於如何[禁用魔術引號](http://php.net/manual/en/security.magicquotes.disabling.php)。你也可以使用['stripslashes'](http://php.net) /manual/en/function.stripslashes.php)在讀取'$ _POST('thisxml')'時手動刪除斜槓,然後將其加載爲XML。 – Andrew

0

我發現這個劇本,只是增加它的頭,瞧:-)

function stripslashes_array(&$array, $iterations=0) { 
    if ($iterations < 3) { 
     foreach ($array as $key => $value) { 
      if (is_array($value)) { 
       stripslashes_array($array[$key], $iterations + 1); 
      } else { 
       $array[$key] = stripslashes($array[$key]); 
      } 
     } 
    } 
} 

if (get_magic_quotes_gpc()) { 
    stripslashes_array($_GET); 
    stripslashes_array($_POST); 
    stripslashes_array($_COOKIE); 
} 

感謝您的輸入;-)