可能重复:
A simple program to CRUD node and node values of xml file
我正在尝试创建一个表单,允许我使用php和xml dom在xml文件中添加新记录。 xml文件在服务器上,我希望客户端能够输入并添加到xml文件。 到目前为止,我的代码是
$data = "books.xml";
$xml = simplexml_load_file($data);
$book = $xml->addChild("book");
$author = $_POST["author"];
$title = $_POST["title"];
$publisher = $_POST["publisher"];
$book->addChild("author", $author);
$book->addChild("title", $title);
$book->addChild("publisher", $publisher);
echo $xml->saveXML();
表格
<form action="addchild.php" method="post">
Name: <input type="text" name="author" />
Email: <input type="text" name="title" />
Comment: <input type="text" name="publisher" />
<input type="submit" />
</form>
XML
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>