PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法
本文實例講述了PHP實現(xiàn)動態(tài)添加XML中數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
前面簡單講述了xml文檔的創(chuàng)建,這里繼續(xù)討論xml中數(shù)據(jù)的動態(tài)添加:
一. 代碼
<?php
class Message_XML extends DomDocument{//定義類Message_XML并繼承DomDocument類
private $Root;
public function __construct(){//構造函數(shù)
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 show_message(){//定義輸出XML文件中的內容函數(shù)
$Root=$this->documentElement;//定義根節(jié)點
$xpath=new DOMXPath($this);//定義DOMXPath
$Node_Record=$this->getElementsByTagName("record");//獲取節(jié)點record的標簽
$Node_Record_length=$Node_Record->length;//獲取標簽的數(shù)量
print"<table width='506' bgcolor='#FFFFCC'><tr>";
print"<td width='106' height='22' align='center'>";
print"<b>用戶名</b>";
print"</td><td width='400' 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語句讀取返回對象中的數(shù)據(jù)
$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='400' align='center'>";
print"$field[2]";
print"</td></tr></table>";
}
}
public function post_message(){//定義輸出表單的函數(shù)
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='添加數(shù)據(jù)'></td><td align='right'><a href='?Action=show_message'>查看數(shù)據(jù)</a> </td></tr></form></table>";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>添加XML數(shù)據(jù)</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" >添加數(shù)據(jù)</a> </td></tr></table></td></tr></table>
<?php
$HawkXML=new Message_XML;//實例化Message_XML類
$Action="";//初始化為空值
if(isset($_GET['Action'])) $Action=$_GET['Action'];
switch($Action){
case "show_message":
$HawkXML->show_message();//當參數(shù)值為show_message時執(zhí)行show_message()函數(shù)
break;
case "post_message":
$HawkXML->post_message();//當參數(shù)值為post_message時執(zhí)行post_message()函數(shù)
break;
case "add_message":
$HawkXML->add_message($_POST['user'],$_POST['address']);//當參數(shù)值為add_message時執(zhí)行add_message()函數(shù)
break;
}
?>
</body>
</html>
二. 運行結果

PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
- PHP解析xml格式數(shù)據(jù)工具類示例
- PHP操作XML作為數(shù)據(jù)庫的類
- PHP中使用xmlreader讀取xml數(shù)據(jù)示例
- PHP生成和獲取XML格式數(shù)據(jù)的方法
- php操作XML、讀取數(shù)據(jù)和寫入數(shù)據(jù)的實現(xiàn)代碼
- php中實現(xiàn)xml與mysql數(shù)據(jù)相互轉換的方法
- php處理復雜xml數(shù)據(jù)示例
- PHP XML備份Mysql數(shù)據(jù)庫
- php操作xml并將其插入數(shù)據(jù)庫的實現(xiàn)方法
- PHP XML數(shù)據(jù)解析代碼
- PHP實現(xiàn)動態(tài)刪除XML數(shù)據(jù)的方法示例
相關文章
探究Laravel使用env函數(shù)讀取環(huán)境變量為null的問題
最近在工作中遇到一個問題,不知道大家有沒有遇到過,在 Laravel中(除 app/config 目錄下的配置文件中)使用env函數(shù)讀取環(huán)境變量,有時有用,有時返回 null,這究竟怎么回事?下面通過這篇文章讓我們一探究竟。有需要的朋友們下面來一起看看吧。2016-12-12
mysql_num_rows VS COUNT 效率問題分析
mysql_num_rows 和 count( * ) 都能統(tǒng)計總數(shù),那個能好一點呢?2011-04-04
php面向對象中static靜態(tài)屬性與方法的內存位置分析
這篇文章主要介紹了php面向對象中static靜態(tài)屬性與方法的內存位置,通過內存位置實例分析了static靜態(tài)屬性的原理與使用技巧,需要的朋友可以參考下2015-02-02

