php xml实例 留言本
前端之家收集整理的这篇文章主要介绍了
php xml实例 留言本,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="codetitle"><a style="CURSOR: pointer" data="85845" class="copybut" id="copybut85845" onclick="doCopy('code85845')"> 代码如下:
<div class="codebody" id="code85845">
<?
PHP //打开用于存储留言的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 "
";
}
?>
<div class="codetitle">
<a style="CURSOR: pointer" data="77559" class="copybut" id="copybut77559" onclick="doCopy('code77559')"> 代码如下: <div class="codebody" id="code77559">
<?
PHP $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);
?>