人人人妻人人人妻人人人,99精品国产综合久久久久五月天 ,欧美白人最猛性XXXXX,日韩AV无码免费播放

News新聞

業(yè)界新聞動態(tài)、技術前沿
Who are we?

您的位置:首頁      樂道系統(tǒng)FAQ      PHP實現(xiàn)動態(tài)刪除XML數據的方法示例

PHP實現(xiàn)動態(tài)刪除XML數據的方法示例

標簽: 發(fā)布日期:2018-04-18 21:57:48 320

本文實例講述了PHP實現(xiàn)動態(tài)刪除XML數據的方法。分享給大家供大家參考,具體如下:

前面介紹了動態(tài)添加XML數據的方法,這里在原有Message_XML類的基礎上稍作改進,實現(xiàn)動態(tài)刪除xml的功能:

一. 代碼

<?php
  class Message_XML extends DomDocument{//定義類Message_XML并繼承DomDocument類
   private $Root;
   public function __construct(){//構造函數
    parent:: __construct();
    if(!file_exists("message.xml")){//判斷文件是否存在
     $xmlstr="<?xml version='1.0' encoding='GB2312'?><message></message>";
     $this->loadXML($xmlstr);
     $this->save("message.xml");//生成XML文檔
    }else{
     $this->load("message.xml");//如果存在則載入XML文檔
    }
   }
   public function add_message($user,$address){//創(chuàng)建方法
    $Root=$this->documentElement;//定義根節(jié)點
    $admin_id=date("Ynjhis");
    $Node_admin_id=$this->createElement("admin_id");//創(chuàng)建節(jié)點admin_id
    $text=$this->createTextNode(iconv("GB2312","UTF-8",$admin_id));//創(chuàng)建一個文本節(jié)點
    $Node_admin_id->appendChild($text);//將文本節(jié)點添加到admin_id節(jié)點中
    $Node_user=$this->createElement("user");//創(chuàng)建節(jié)點user
    $text=$this->createTextNode(iconv("GB2312","UTF-8",$user));//創(chuàng)建一個文本節(jié)點
    $Node_user->appendChild($text);//將文本節(jié)點添加到user節(jié)點中
    $Node_address=$this->createElement("address");//創(chuàng)建節(jié)點address
    $text=$this->createTextNode(iconv("GB2312","UTF-8",$address));//創(chuàng)建一個文本節(jié)點
    $Node_address->appendChild($text);//將文本節(jié)點添加到address節(jié)點中
    $Node_Record=$this->createElement("record");//創(chuàng)建節(jié)點record
    $Node_Record->appendChild($Node_admin_id);//將admin_id節(jié)點添加到record節(jié)點中
    $Node_Record->appendChild($Node_user);//將user節(jié)點添加到record節(jié)點中
    $Node_Record->appendChild($Node_address);//將address節(jié)點添加到record節(jié)點中
    $Root->appendChild($Node_Record);//將record節(jié)點添加到根節(jié)點中
    $this->save("message.xml");//生成XML文檔
    echo "<script>alert('添加成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
   }
   public function delete_message($admin_id){//定義刪除函數
    $Root=$this->documentElement;//定義根節(jié)點
    $xpath=new DOMXPath($this);//定義DOMXPath
    $Node_Record=$xpath->query("http://record[admin_id='$admin_id']");//執(zhí)行查詢語句
    $Root->removeChild($Node_Record->item(0));//刪除節(jié)點
    $this->save("message.xml");//生成XML文檔
    echo "<script>alert('刪除成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
   }
   public function show_message(){//定義輸出XML文件中的內容函數
    $Root=$this->documentElement;//定義根節(jié)點
    $xpath=new DOMXPath($this);//定義DOMXPath
    $Node_Record=$this->getElementsByTagName("record");//獲取節(jié)點record的標簽
    $Node_Record_length=$Node_Record->length;//獲取標簽的數量
    print"<table width='506' bgcolor='#FFFFCC'><tr>";
    print"<td width='106' height='22' align='center'>";
    print"<b>用戶名</b>";
    print"</td><td width='300' align='center'>";
    print"<b>留言信息</b></td></tr>";
    for($i=0;$i<$Node_Record->length;$i++){//應用for循環(huán)輸出查詢結果
     $k=0;
     foreach($Node_Record->item($i)->childNodes as $articles){  //通過foreach語句讀取返回對象中的數據
      $field[$k]=iconv("UTF-8","GB2312",$articles->textContent);//實現(xiàn)編碼格式的轉換
      $k++;
     }
     print"<table width='506' bgcolor='#FFFFCC'><tr>";
     print"<td width='106' height='22' align='center'>";
     print"$field[1]";
     print"</td><td width='300' align='center'>";
     print"$field[2]";
     print"</td><td width='100' align='center'>";
     print"<a href='?Action=delete_message&admin_id=$field[0]'>刪除</a></td>";
     print"</tr></table>";
    }
   }
   public function post_message(){
    print"<table width='506' bgcolor='#FFFFCC'><form method='post' action='?Action=add_message'>";
    print"<tr><td width='106' height='22'>    用戶名:</td><td><input type='text' name='user' size=50></td></tr>";
    print"<tr><td width='106' height='22'>    留言信息:</td><td width='400'><textarea name='address' cols='48' rows='5' id='address'></textarea></td></tr>";
    print"<tr><td width='106' height='30'>  <input type='submit' value='添加數據'></td><td align='right'><a href='?Action=show_message'>查看數據</a>    </td></tr></form></table>";
   }
  }
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>刪除XML數據</title>
<style type="text/css">
<!--
body,td,th {
  font-size: 12px;
}
-->
</style></head>
<body>
<table width="506" height="50" border="0" cellpadding="0" cellspacing="0" bgcolor="#33BE6B">
 <tr>
  <td width="506" height="50" valign="bottom" background="title.gif">
   <table width="506">
    <tr>
     <td height="24" align="right" scope="col">  <a href="?Action=post_message" rel="external nofollow" >添加數據</a>   </td></tr></table></td></tr>
     <?php
      $HawkXML=new Message_XML;//實例化Message_XML類
      $Action="";//初始化為空值
      if(isset($_GET['Action'])) $Action=$_GET['Action'];
      switch($Action){
       case "show_message":
       $HawkXML->show_message();//當參數值為show_message時執(zhí)行show_message()函數
       break;
       case "post_message":
       $HawkXML->post_message();//當參數值為post_message時執(zhí)行post_message()函數
       break;
       case "add_message":
       $HawkXML->add_message($_POST['user'],$_POST['address']);//當參數值為add_message時執(zhí)行add_message()函數
       break;
       case "delete_message":
       $HawkXML->delete_message($_GET['admin_id']);//當參數值為delete_message時執(zhí)行delete_message()函數
       break;
      }
     ?>
</table>
</body>
</html>