This example is used to learn how to create an xml message book.
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) // read every thread tag in XML data cyclically
{
Echo"Title:". $ Th-> title ."
";
Echo"Author:". $ Th-> author ."
";
Echo"Content:
".$th->content."
";
Echo "";
}
?>
The Code is as follows:
$ Guestbook = new DomDocument (); // create a new DOM object
$ Guestbook-> load ('db/guestbook. xml'); // read xml data
$ Threads = $ guestbook-> documentElement; // obtain 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 );
// Create the author label on the new thread Node
$ Author = $ guestbook-> createElement ('author ');
$ Author-> appendChild ($ guestbook-> createTextNode ($ _ POST ['author']);
$ Thread-> appendChild ($ author );
// Create the content tag on the new thread Node
$ Content = $ guestbook-> createElement ('content ');
$ Content-> appendChild ($ guestbook-> createTextNode ($ _ POST ['content']);
$ Thread-> appendChild ($ content );
// Write XML data to a file
$ Fp = fopen ("DB/guestbook. xml", "w ");
If (fwrite ($ fp, $ guestbook-> saveXML ()))
Echo "message submitted successfully ";
Else
Echo "message submission failed ";
Fclose ($ fp );
?>
The Code is as follows:
Http://www.w3.org/TR/html4/loose.dtd>
Post New Messages
Post New Messages