>  기사  >  php教程  >  php xml实例 留言本

php xml实例 留言本

WBOY
WBOY원래의
2016-06-13 12:24:151346검색

复制代码 代码如下:


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


发表新的留言



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.