Home >Backend Development >PHP Tutorial >php xml example guestbook_PHP tutorial

php xml example guestbook_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:46:53884browse

Copy code The code is as follows:

//Open the XML file used to store messages
$ guestbook = simplexml_load_file('DB/guestbook.xml');

foreach($guestbook->thread as $th) //Loop to read each thread tag in the XML data
{
echo "Title: ".$th->title."
";
echo "Author: ".$th ->author."
";
echo "Content:
".$th->content."
";
echo "
";
}
?>

Copy code The code is as follows:

$guestbook = new DomDocument(); //Create a new DOM object
$guestbook->load('DB/guestbook.xml'); //Read XML data
$threads = $guestbook->documentElement; //Get the root of the XML structure
//Create a new thread node
$thread = $guestbook->createElement(' thread');
$threads->appendChild($thread);
//Create the title tag on the new thread node
$title = $guestbook->createElement('title');
$title->appendChild($guestbook->createTextNode($_POST['title']));
$thread->appendChild($title);
//In new thread Create the author tag on the node
$author = $guestbook->createElement('author');
$author->appendChild($guestbook->createTextNode($_POST['author']));
$thread->appendChild($author);
//Create content tag on new thread node
$content = $guestbook->createElement('content');
$ content->appendChild($guestbook->createTextNode($_POST['content']));
$thread->appendChild($content);
//Write XML data to file
$fp = fopen("DB/guestbook.xml", "w");
if(fwrite($fp, $guestbook->saveXML()))
echo "Message submitted successfully";
else
echo "Message submission failed";
fclose($fp);
?>

Copy code The code is as follows:

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


Post a new comment











< td>Author

< /tr>




title
Content






< ;/body>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320036.htmlTechArticleCopy the code as follows: ?php //Open the XML file used to store messages $guestbook = simplexml_load_file('DB /guestbook.xml'); foreach($guestbook-thread as $th) //Loop to read XML data...
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