php写xml文件老是出错,一时找不到原因, 请求大伙帮助!
以下是我的程序代码:
<?php>
$txtmail = $_POST["txtemail"];
$txtname = $_POST["txtname"];
$txtmsg = $_POST["txtmessage"];$doc = newDOMDocument("1.0");
$doc->load("xmlorders.xml");
$root=$doc->getElementsByTagName("orders")->item(0);
$order = $doc->createElement("order");
$name = $doc->createElement("name");
$email = $doc->createElement("email");
$message = $doc->createElement("message");
$date = $doc->createElement("createdate");$name->appendChild($doc->createTextNode($txtname));
$email->appendChild($doc->createTextNode($txtmail));
$message->appendChild($doc->createTextNode($txtmsg));
$date->appendChild($doc->createTextNode(date("Y-m-j")+" "+date("G:i:s")));$order->appendChild($name);
$order->appendChild($email);
$order->appendChild($message);
$order->appendChild($date);
$root->appendChild($order);
//echo$doc->saveXML();
$doc->save("xmlorders.xml");
?>是不是xml文件必须得到物理路径?
谢谢!
以下是我的程序代码:
<?php>
$txtmail = $_POST["txtemail"];
$txtname = $_POST["txtname"];
$txtmsg = $_POST["txtmessage"];$doc = newDOMDocument("1.0");
$doc->load("xmlorders.xml");
$root=$doc->getElementsByTagName("orders")->item(0);
$order = $doc->createElement("order");
$name = $doc->createElement("name");
$email = $doc->createElement("email");
$message = $doc->createElement("message");
$date = $doc->createElement("createdate");$name->appendChild($doc->createTextNode($txtname));
$email->appendChild($doc->createTextNode($txtmail));
$message->appendChild($doc->createTextNode($txtmsg));
$date->appendChild($doc->createTextNode(date("Y-m-j")+" "+date("G:i:s")));$order->appendChild($name);
$order->appendChild($email);
$order->appendChild($message);
$order->appendChild($date);
$root->appendChild($order);
//echo$doc->saveXML();
$doc->save("xmlorders.xml");
?>是不是xml文件必须得到物理路径?
谢谢!
function addOrder($doc, $value)
{
$item = $doc->createElement('order');
$cdata = $doc->createCDATASection($value);
$item->appendChild($cdata);
$doc->documentElement->appendChild($item);
}
$txtmail = $_POST["txtemail"];
$txtname = $_POST["txtname"];
$txtmsg = $_POST["txtmessage"];
$doc = new DOMDocument();
$doc->loadXML('<orders></orders>');addOrder($doc, $txtmail);
addOrder($doc, $txtname);
addOrder($doc, $txtmsg);$doc->save('D:/test.xml');
<?php
$txtmail = "txtemail";
$txtname = "txtname";
$txtmsg = "txtmessage";$doc = new DOMDocument("1.0");
$doc->load("xmlorders.xml");
$root=$doc->getElementsByTagName("orders")->item(0);
$order = $doc->createElement("order");
$name = $doc->createElement("name");
$email = $doc->createElement("email");
$message = $doc->createElement("message");
$date = $doc->createElement("createdate");$name->appendChild($doc->createTextNode($txtname));
$email->appendChild($doc->createTextNode($txtmail));
$message->appendChild($doc->createTextNode($txtmsg));
$date->appendChild($doc->createTextNode(date("Y-m-j")+" "+date("G:i:s")));$order->appendChild($name);
$order->appendChild($email);
$order->appendChild($message);
$order->appendChild($date);
$root->appendChild($order);
//echo$doc->saveXML();
$doc->save("xmlorders.xml");
?>
$txtmail = $_POST["txtemail"];
$txtname = $_POST["txtname"];
$txtmsg = $_POST["txtmessage"];if(file_exists('test.xml') === false)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp, '<?xml version="1.0" encoding="UTF-8" ?><orders></orders>');
fclose($fp);
}$doc = new DOMDocument('1.0', 'UTF-8');
$doc->load('test.xml');$order = $doc->createElement('order');$name = $doc->createElement("name");
$email = $doc->createElement("email");
$message = $doc->createElement("message");
$date = $doc->createElement("createdate");$name->appendChild($doc->createCDATASection($txtname));
$email->appendChild($doc->createCDATASection($txtmail));
$message->appendChild($doc->createCDATASection($txtmsg));
$date->appendChild($doc->createCDATASection(date("Y-m-j")+" "+date("G:i:s")));$order->appendChild($name);
$order->appendChild($email);
$order->appendChild($message);
$order->appendChild($date);$doc->documentElement->appendChild($order);$doc->save('test.xml');
这个已经测试过...
<?xml version="1.0" encoding="UTF-8" ?>
- <orders>
- <order>
- <name>
- <![CDATA[ my name
]]>
</name>
- <email>
- <![CDATA[ my mail
]]>
</email>
- <message>
- <![CDATA[ my message
]]>
</message>
- <createdate>
- <![CDATA[ 2024
]]>
</createdate>
</order>
</orders>