PHP实现动态删除XML数据的方法示例
作者:chengqiuming 时间:2024-06-05 09:51:28
本文实例讲述了PHP实现动态删除XML数据的方法。分享给大家供大家参考,具体如下:
前面介绍了动态添加XML数据的方法,这里在原有Message_XML类的基础上稍作改进,实现动态删除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){//创建方法
$Root=$this->documentElement;//定义根节点
$admin_id=date("Ynjhis");
$Node_admin_id=$this->createElement("admin_id");//创建节点admin_id
$text=$this->createTextNode(iconv("GB2312","UTF-8",$admin_id));//创建一个文本节点
$Node_admin_id->appendChild($text);//将文本节点添加到admin_id节点中
$Node_user=$this->createElement("user");//创建节点user
$text=$this->createTextNode(iconv("GB2312","UTF-8",$user));//创建一个文本节点
$Node_user->appendChild($text);//将文本节点添加到user节点中
$Node_address=$this->createElement("address");//创建节点address
$text=$this->createTextNode(iconv("GB2312","UTF-8",$address));//创建一个文本节点
$Node_address->appendChild($text);//将文本节点添加到address节点中
$Node_Record=$this->createElement("record");//创建节点record
$Node_Record->appendChild($Node_admin_id);//将admin_id节点添加到record节点中
$Node_Record->appendChild($Node_user);//将user节点添加到record节点中
$Node_Record->appendChild($Node_address);//将address节点添加到record节点中
$Root->appendChild($Node_Record);//将record节点添加到根节点中
$this->save("message.xml");//生成XML文档
echo "<script>alert('添加成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
public function delete_message($admin_id){//定义删除函数
$Root=$this->documentElement;//定义根节点
$xpath=new DOMXPath($this);//定义DOMXPath
$Node_Record=$xpath->query("//record[admin_id='$admin_id']");//执行查询语句
$Root->removeChild($Node_Record->item(0));//删除节点
$this->save("message.xml");//生成XML文档
echo "<script>alert('删除成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
public function show_message(){//定义输出XML文件中的内容函数
$Root=$this->documentElement;//定义根节点
$xpath=new DOMXPath($this);//定义DOMXPath
$Node_Record=$this->getElementsByTagName("record");//获取节点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循环输出查询结果
$k=0;
foreach($Node_Record->item($i)->childNodes as $articles){ //通过foreach语句读取返回对象中的数据
$field[$k]=iconv("UTF-8","GB2312",$articles->textContent);//实现编码格式的转换
$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时执行show_message()函数
break;
case "post_message":
$HawkXML->post_message();//当参数值为post_message时执行post_message()函数
break;
case "add_message":
$HawkXML->add_message($_POST['user'],$_POST['address']);//当参数值为add_message时执行add_message()函数
break;
case "delete_message":
$HawkXML->delete_message($_GET['admin_id']);//当参数值为delete_message时执行delete_message()函数
break;
}
?>
</table>
</body>
</html>
二. 运行结果
希望本文所述对大家PHP程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/78600673
标签:PHP,XML
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JS实现点击li标签弹出对应的索引功能【案例】
2024-04-17 10:24:23
![](https://img.aspxhome.com/file/2023/8/136298_0s.gif)
python通过Seq2Seq实现闲聊机器人
2021-09-02 13:39:15
![](https://img.aspxhome.com/file/2023/4/113034_0s.png)
Oracle 创建监控账户 提高工作效率
2009-10-14 11:47:00
![](https://img.aspxhome.com/file/UploadPic/200910/14/20091014121213816.jpg)
PHP字符串中提取文件名的实例方法
2023-06-12 18:57:32
Python实现8种常用抽样方法
2023-02-01 18:03:19
![](https://img.aspxhome.com/file/2023/7/65487_0s.png)
理解JavaScript变量作用域更轻松
2024-04-16 09:25:07
python安装pil库方法及代码
2021-10-22 16:55:24
![](https://img.aspxhome.com/file/2023/6/133356_0s.png)
Oracle例外用法实例详解
2024-01-16 21:46:23
谈谈网页设计中的字体应用 (4) 实战应用篇·下
2009-11-24 13:13:00
![](https://img.aspxhome.com/file/UploadPic/200911/24/202939796-23s.gif)
python源文件的字符编码知识点详解
2021-04-30 08:05:04
oracle 中 sqlplus命令大全
2024-01-23 21:07:27
Python cookbook(数据结构与算法)实现查找两个字典相同点的方法
2022-07-20 22:09:46
![](https://img.aspxhome.com/file/2023/9/130659_0s.png)
Vue实现自定义视频和图片上传的示例代码
2024-05-21 10:30:21
python调用staf自动化框架的方法
2021-11-03 17:47:43
Python使用sqlite3模块内置数据库
2024-01-26 21:51:39
![](https://img.aspxhome.com/file/2023/3/79613_0s.png)
关于设计品质保证(DQA)的几点想法
2007-11-16 16:55:00
Python中用字符串调用函数或方法示例代码
2023-03-05 15:37:59
Python pip安装模块提示错误解决方案
2022-03-07 02:26:27
![](https://img.aspxhome.com/file/2023/0/115450_0s.png)
python之线程池map()方法传递多参数list
2022-06-13 09:00:55
![](https://img.aspxhome.com/file/2023/6/131226_0s.jpg)
Laravel框架执行原生SQL语句及使用paginate分页的方法
2023-11-24 00:27:24