複製代碼 代碼如下:
//開啟用於儲存留言的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">
發表新的留言
發表新的留言