我有一个textarea,我在其中打印所有我的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"?>
我尝试用PHP保存这样的xml:
$myFile = 'TEST.xml';
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = htmlspecialchars($_POST['thisxml']);
fwrite($fh, $stringData);
fclose($fh);
有人可以告诉我我做错了什么吗?
提前致谢; - )
答案 0 :(得分:3)
使用htmlspecialchars($_POST['thisxml'])
会使您的XML invalid
返回类似
<?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>
只需使用file_put_contents
,它就会结合fopen , fwrite , fclose
file_put_contents('TEST.xml', $_POST['thisxml']);
答案 1 :(得分: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");
}
答案 2 :(得分: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);
}
感谢您的意见; - )