代码如下:
//打开用于存储留言的XML文件
$guestbook = simplexml_load_file('DB/guestbook.xml');
foreach($guestbook->thread as $th) //循环读取XML数据中的每一个thread标签
{
echo "
标题:".$th->title."
";
echo "
作者:".$th->author."
";
echo "
内容:".$th->content."
";
echo "
";
}
?>
代码如下:
$guestbook = new DomDocument(); //创建一个新的DOM对象
$guestbook->load('DB/guestbook.xml'); //读取XML数据
$threads = $guestbook->documentElement; //获得XML结构的根
//创建一个新thread节点
$thread = $guestbook->createElement('thread');
$threads->appendChild($thread);
//在新的thread节点上创建title标签
$title = $guestbook->createElement('title');
$title->appendChild($guestbook->createTextNode($_POST['title']));
$thread->appendChild($title);
//在新的thread节点上创建author标签
$author = $guestbook->createElement('author');
$author->appendChild($guestbook->createTextNode($_POST['author']));
$thread->appendChild($author);
//在新的thread节点上创建content标签
$content = $guestbook->createElement('content');
$content->appendChild($guestbook->createTextNode($_POST['content']));
$thread->appendChild($content);
//将XML数据写入文件
$fp = fopen("DB/guestbook.xml", "w");
if(fwrite($fp, $guestbook->saveXML()))
echo "留言提交成功";
else
echo "留言提交失败";
fclose($fp);
?>
代码如下:
"http://www.w3.org/TR/html4/loose.dtd">
发表新的留言 发表新的留言