Phpxml instance message book _ php instance

Source: Internet
Author: User
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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.