PHP XML Instance message book _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Open the XML file for storing messages
$guestbook = simplexml_load_file (' Db/guestbook.xml ');

foreach ($guestbook->thread as $th)//iterate through each thread label in the XML data
{
echo " Title:". $th->title."
";
echo " Author:". $th->author."
";
echo " content:
". $th->content."
";
echo "";
}
?>

Copy CodeThe code is as follows:
$guestbook = new DomDocument (); To create a new DOM object
$guestbook->load (' db/guestbook.xml '); Reading 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 a title tag on the new thread node
$title = $guestbook->createelement (' title ');
$title->appendchild ($guestbook->createtextnode ($_post[' title '));
$thread->appendchild ($title);
Create a author label on the new thread node
$author = $guestbook->createelement (' author ');
$author->appendchild ($guestbook->createtextnode ($_post[' author '));
$thread->appendchild ($author);
Create a content label on a new thread node
$content = $guestbook->createelement (' content ');
$content->appendchild ($guestbook->createtextnode ($_post[' content '));
$thread->appendchild ($content);
Writing 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);
?>

Copy CodeThe code is as follows:
"Http://www.w3.org/TR/html4/loose.dtd" >


<title>Post a new message</title>



Post a new message





http://www.bkjia.com/PHPjc/320036.html www.bkjia.com true http://www.bkjia.com/PHPjc/320036.html techarticle Copy the code as follows:? PHP//Open the XML file used to store the message $guestbook = simplexml_load_file (' Db/guestbook.xml '); foreach ($guestbook-thread As $th)//loop-read XML data ...

  • 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.