Home >php教程 >php手册 >php xml实例 留言本

php xml实例 留言本

WBOY
WBOYOriginal
2016-06-13 12:24:151410browse

复制代码 代码如下:


//打开用于存储留言的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);
?>


复制代码 代码如下:


BR>"http://www.w3.org/TR/html4/loose.dtd">


发表新的留言



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn