我在本地建了三个文件,一个静态表单文件,还有一个php脚本文件,另外还有一个xml文件,三个文件的具体代码如下:post.php
<?php
 $guestbook=new DomDocument();
 //创建一个新的DOMain对象
 $guestbook->load('db/gusetbook.xml');
 //读取XML数据
 $threads=$guestbook->documentElement;
 //获得XML结构的根
 //下面创建一个新thread节点
 $thread=$gusetbook->createElement('thread');
 $threads->appendChild($thread);
 //在新的thread节点上创建title标签
 $title=$guestbook->createElement('title');
 $title->appendChild($gusetbook->createTextNode($_POST['title']));
 $thread->appendChild($title);
 //在新的thread节点上创建author标签 
 $author=$guestbook->createElement('author');
 $author->appendChild($gusetbook->createTextNode($_POST['author']));
 $thread->appendChild($author);
 //在新的thread节点上创建content标签
 $content=$guestbook->createElement('content');
 $content->appendChild($gusetbook->createTextNode($_POST['content']));
 $thread->appendChild($content);
 //将XML数据写入文件
 $fp=fopen("DB/guestbook.xml","w");
 if(fwrite($fp,$guestbook->saveXML()))
   echo "留言提交成功";
 else
   echo "留言提交失败";
 fclose($fp);
?> insert.html
<html>
 <head>
  <title>发表新的留言</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 </head>
 
 <body>
  <h1><p align="center">发表新的留言</p></h1>
  <form name="form1" method="post" action="post.php">
   <table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
     <td>标题</td>
     <td><input type="text" name="title" id="title" size="50"></td>
    </tr>
    <tr>
     <td>作者</td>
     <td><input type="text" name="author" id="author" size="20"></td>
    </tr>
                           <tr>
     <td>内容</td>
     <td><textarea name="content" cols="50" rows="10" id="content"></textarea></td>
    </tr>
    <tr>
     <td colspan="2" align="center"><input type="submit" name="submit" value="提交">
     <input type="reset" name="reset" value="重置"></td>
    </tr>
   </table>
  </form>
 </body>
</html> guestbook.xml
<?xml version="1.0"?>
<threads>
 <thread>
  <title>这里是留言的标题</title>
  <author>这里是留言者</author>
  <content>这里是留言内容</content>
  </thread>
</threads>

解决方案 »

  1.   

    XML没有指定字符集?好歹也说说什么错误嘛。
      

  2.   

    Warning: DOMDocument::load() [function.DOMDocument-load]: I/O warning : failed to load external entity "/home/iranjone/domains/×/public_html/liuyan/DB/guestbook.xml" in /home/iranjone/domains/×/public_html/liuyan/Post.php on line 3Fatal error: Call to a member function appendChild() on a non-object in /home/iranjone/domains/×/public_html/liuyan/Post.php on line 7
    这个是错误!!