我正在尝试创建一个表单,允许我使用 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>