PHP XML Instance Message _php instance

Source: Internet
Author: User
Copy Code code as follows:

<?php
Open an XML file for storing messages
$guestbook = simplexml_load_file (' Db/guestbook.xml ');

foreach ($guestbook->thread as $th)//loop read every thread label in XML data
{
echo "<B> title:</b>". $th->title. " <BR> ";
echo "<B> author:</b>". $th->author. " <BR> ";
echo "<B> content:</b><pre>". $th->content. " </PRE> ";
echo "<HR>";
}
?>

Copy Code code as follows:

<?php
$guestbook = new DOMDocument (); To create a new DOM object
$guestbook->load (' db/guestbook.xml '); Reading XML data
$threads = $guestbook->documentelement; Get root of XML structure
Create a new thread node
$thread = $guestbook->createelement (' thread ');
$threads->appendchild ($thread);
Create a title tag on a 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 Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >


<title> post a new message </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<form name= "Form1" method= "Post" action= "post.php" >
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
<td> title </td>
<td><input name= "title" type= "text" id= "title" Size= "M" ></td>
</tr>
<tr>
<td> author </td>
<td><input name= "Author" type= "text" id= "author" size= "></td>"
</tr>
<tr>
<td> content </td>
<td><textarea name= "Content" cols= "rows=" id= "content" ></textarea></td>
</tr>
</table>
<p align= "center" >
<input type= "Submit" value= "Submit" >
<input type= "reset" value= "reset" >
</p>
</form>
</body>

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.