我经历了大约3个小时没有成功。有人能指出我脚本有什么问题吗?它写的信息,但它在一行,我正在形成它,尝试以不同的方式,没有任何东西只是保持在一行。
<?php
if (!file_exists ($fpath))
{
$fo = fopen($fpath, 'a') or die("Failed to create file.");
fwrite($fo, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r<".$fname.">\r</".$fname.">") or die("Failed to format file.");
fclose($fo);
}
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load($fpath);
$date = date("Y-m-d");
@$email = $_POST['email'];
$nonode = $xml->getElementsByTagName($sn);
if ($nonode->length == 0)
{
$addnode = $xml->createElement($sn);
$newnode = $addnode->appendChild($xml->createElement('data'));
$newnode->appendChild($xml->createElement('date', $date));
if (isset ($_POST['email']))
{
$newnode->appendChild($xml->createElement('email', $email));
}
$xml->getElementsByTagName($fname)->item(0)->appendChild($addnode);
$xml->save($fpath);
}
?>
答案 0 :(得分:2)
玩弄它,让它按照需要运行。在通过创建XML元素检查fopen()
时,替换fwrite
,file_exists()
,生成的脚本如下所示:
<?php
if (!file_exists ($fpath))
{
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$rootnode = $xml->createElement($fname);
$addnode = $rootnode->appendChild($xml->createElement($sn));
$newnode = $addnode->appendChild($xml->createElement('data'));
if (isset ($_POST['email']))
{
$newnode->appendChild($xml->createElement('email', $email));
}
$xml->appendChild($rootnode);
$xml->save($fpath);
}
?>
输出漂亮:
<?xml version="1.0" encoding="UTF-8"?>
<expdb>
<sn>
<data>
<email>myemail@email.com</email>
</data>
</sn>
</expdb>
感谢所有人,并希望这可以帮助某人。您应该将此作为参考,记下XML标记名称和XML函数参数。