php基礎(chǔ)教程-XML資料_第1頁
php基礎(chǔ)教程-XML資料_第2頁
php基礎(chǔ)教程-XML資料_第3頁
php基礎(chǔ)教程-XML資料_第4頁
php基礎(chǔ)教程-XML資料_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

php基礎(chǔ)教程-XML一創(chuàng)建XML文件index.php:<?xmlversion="1.0"encoding="GB2312"?><?xml-stylesheettype=“text/css”href=“style.css”?><!--這是XML文檔的注釋--><圖書管理系統(tǒng)><管理員><用戶名>李5</用戶名> <編號>0101</編號> <電話>12345678</電話></管理員><管理員><用戶名>王5</用戶名> <編號>0102</編號> <電話>92345678</電話></管理員></圖書管理系統(tǒng)>二讀取XML文件index.php:<?phpclassMessage_XMLextendsDOMDocument{//創(chuàng)建一個(gè)類private$root;//構(gòu)造函數(shù)publicfunction__construct(){parent::__construct();$this->load("message.xml");}//輸出XML文件中的內(nèi)容publicfunctionshow_message(){$root=$this-documentElement; $xpath=newDOMXPath($this); $node_record=$this->getelementsByTagName("record"); $node_record_length=$node_record->length; print"<tablewidth='450'bgcolor='#97F7ED'><tr>";print"<tdwidth='150'height='22'align='center'>"; print"<b>用戶名</b>"; print"</td><tdwidth='150'align='center'>";print"<b>密碼</b>"; print"</td><tdwidth='150'align='center'>"; print"<b>地址</b>"; print"</td></tr>"; for($i=0;$i<$node_record->length;$i++){ $k=0; foreach($node_record->item($i)->childNodesas$articles){ $field[$k]=iconv("UTF-8","GB2312",$articles->textContent); $k++; } print"<tablewidth='450'bgcolor='#97F7ED'><tr>"; print"<tr><tdwidth='150'height='22'align='center'>"; print"$field[0]"; print"</td><tdwidth='150'align='center'>";print"$field[1]"; print"</td><tdwidth='150'align='center'>"; print"$field[2]"; print"</tr></table>"; }}}?><html><head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><title>讀取XML文件</title><styletype="text/css"><!--body,td,th{ font-size:13px;}a:link{ text-decoration:none;}a:visited{ text-decoration:none;}a:hover{ text-decoration:none;}a:active{ text-decoration:none;}.STYLE2{font-size:18px}--></style></head><body><tablewidth="450"height="50"border="0"cellpadding="0"cellspacing="0"><tr><tdheight="50"align="center"bgcolor="#97F7ED"><ahref=?Action=show_messageclass="STYLE2">讀取XML文件</a></td></tr><?php$hawkXML=newMessage_XML;$Action=$_GET['Action'];switch($Action){case"show_message": $hawkXML->show_message(); break;}?></table><tablewidth="450"height="50"border="0"cellpadding="0"cellspacing="0"><tr><tdalign="right"bgcolor="#97F7ED"><ahref="message.xml">查看XML文件內(nèi)容</a>    </td></tr></table></body></html>message.xml:<?xmlversion="1.0"encoding="GB2312"?><管理員><record><user>清風(fēng)</user><pass>123456</pass><address>長春市</address></record><record><user>明日</user><pass>456789</pass><address>長春市</address></record><record><user>歐陽正</user><pass>789456</pass><address>樓藍(lán)古國</address></record><record><user>南天門</user><pass>123123</pass><address>天宮</address></record></管理員>三添加XML文件index.php:<?php //Message_XML類,繼承PHP5的DomDocument類 classMessage_XMLextendsDomDocument{ //屬性 private$Root; //方法 //構(gòu)造函數(shù) publicfunction__construct(){ parent::__construct(); //創(chuàng)建或讀取存儲(chǔ)留言信息的XML文檔message.xml if(!file_exists("message.xml")){ $xmlstr="<?xmlversion='1.0'encoding='GB2312'?><message></message>"; $this->loadXML($xmlstr); $this->save("message.xml"); } else $this->load("message.xml");}publicfunctionadd_message($user,$pass,$address){//添加數(shù)據(jù) $Root=$this->documentElement; //獲取留言消息 $admin_id=date("Ynjhis"); $Node_admin_id=$this->createElement("admin_id"); $text=$this->createTextNode(iconv("GB2312","UTF-8",$admin_id)); $Node_admin_id->appendChild($text); $Node_user=$this->createElement("user"); $text=$this->createTextNode(iconv("GB2312","UTF-8",$user)); $Node_user->appendChild($text); $Node_pass=$this->createElement("pass"); $text=$this->createTextNode(iconv("GB2312","UTF-8",$pass)); $Node_pass->appendChild($text); $Node_address=$this->createElement("address"); $text=$this->createTextNode(iconv("GB2312","UTF-8",$address)); $Node_address->appendChild($text); $Node_Record=$this->createElement("record"); $Node_Record->appendChild($Node_admin_id); $Node_Record->appendChild($Node_user); $Node_Record->appendChild($Node_pass); $Node_Record->appendChild($Node_address); //加入到根結(jié)點(diǎn)下 $Root->appendChild($Node_Record); $this->save("message.xml"); echo"<script>alert('添加成功');location.href='".$_SERVER['PHP_SELF']."'</script>";}publicfunctionshow_message(){//讀取數(shù)據(jù)$root=$this-documentElement; $xpath=newDOMXPath($this); $Node_Record=$this->getElementsByTagName("record"); $Node_Record_length=$Node_Record->length; print"<tablewidth='350'bgcolor='#97F7ED'><tr>";print"<tdwidth='115'height='22'align='center'>"; print"<b>用戶名</b>"; print"</td><tdwidth='115'align='center'>";print"<b>密碼</b>"; print"</td><tdwidth='120'align='center'>"; print"<b>地址</b></td></tr>"; for($i=0;$i<$Node_Record->length;$i++){ $k=0; foreach($Node_Record->item($i)->childNodesas$articles){ $field[$k]=iconv("UTF-8","GB2312",$articles->textContent); $k++; } print"<tablewidth='350'bgcolor='#97F7ED'><tr>"; print"<tdwidth='115'height='22'align='center'>"; print"$field[1]"; print"</td><tdwidth='115'align='center'>";print"$field[2]"; print"</td><tdwidth='120'align='center'>"; print"$field[3]"; print"</td>"; print"</tr></table>"; }} publicfunctionpost_message(){ print"<tablewidth='350'bgcolor='#97F7ED'><formmethod='post'action='?Action=add_message'>"; print"<tr><tdwidth='100'height='22'align='center'>用戶名:</td><td><inputtype=textname='user'size=30></td></tr>"; print"<tr><tdwidth='100'height='22'align='center'>密碼:</td><td><inputtype=textname='pass'size=30></td></tr>"; print"<tr><tdwidth='100'height='22'align='center'>地址:</td><td><inputtype=textname='address'size=30></td></tr>"; print"<tr><tdwidth='100'height='22'align='center'><inputtype='submit'value='添加數(shù)據(jù)'></td><tdalign='right'><ahref=?Action=show_message>查看數(shù)據(jù)</a>    </td></tr></form></table>"; }}?><html> <head> <title>添加XML數(shù)據(jù)</title> <style>td,body{font-size:14 px}a:link{ text-decoration:none;}a:visited{ text-decoration:none;}a:hover{ text-decoration:none;}a:active{ text-decoration:none;}</style> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"></head><body><tablewidth=350height=35border=0cellpadding=0cellspacing=0bgcolor="#97F7ED"> <tr> <tdheight=35align="center">  <ahref=?Action=post_message>添加數(shù)據(jù)</a></td> </tr> <?php $HawkXML=newMessage_XML; $Action=""; if(isset($_GET['Action'])) $Action=$_GET['Action']; switch($Action){ case"show_message": //查看 $HawkXML->show_message(); break; case"post_message"://提交 $HawkXML->post_message(); break; case"add_message"://添加 $HawkXML->add_message($_POST['user'],$_POST['pass'],$_POST['address']); break; } ?></table></body> </html>message.xml:<?xmlversion="1.0"encoding="GB2312"?><message><record><admin_id>2007424065018</admin_id><user>英特爾</user><pass>123456</pass><address>長春市</address></record><record><admin_id>2007424065056</admin_id><user>網(wǎng)易論壇</user><pass>789465</pass><address>長春市</address></record><record><admin_id>2007424065133</admin_id><user>網(wǎng)易軟件</user><pass>456456</pass><address>長春市</address></record><record><admin_id>2007426050235</admin_id><user>mrsoft</user><pass>123456</pass><address>長春市</address></record></message>四查詢XML文件index.php:<?php classMessage_XMLextendsDomDocument{ private$Root; publicfunction__construct(){ parent::__construct(); $this->load("message.xml");}publicfunctionselect_message($Action){ $Root=$this->documentElement; $xpath=newDOMXPath($this); $Node_Record=$xpath->query("http://record[admin_id='$Action']"); $g=0; foreach($Node_Record->item(0)->childNodesas$node){ $field[$g]=iconv("UTF-8","GB2312",$node->textContent); $g++; } print"<tablewidth='450'border=1cellpadding=3cellspacing=1bgcolor='#00CCFF'>"; print"<tr><tdwidth='95'align='right'>用戶名:</td><td><inputtype=textname='user'value='$field[1]'size=30></td></tr>"; print"<tr><tdwidth='95'align='right'>密碼:</td><td><inputtype=textname='pass'value='$field[2]'size=30></td></tr>"; print"<tr><tdwidth='95'align='right'>地址:</td><td><inputtype=textname='address'value='$field[3]'size=30></td></tr>"; print"</table>";} }?><html><head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><title>查詢XML文件</title><styletype="text/css"><!--body,td,th{ font-size:13px;}a:link{ text-decoration:none;}a:visited{ text-decoration:none;}a:hover{ text-decoration:none;}a:active{ text-decoration:none;}.STYLE2{font-size:18px}--></style></head><body><tablewidth=450height=35border=1cellpadding=3cellspacing=1bgcolor="#00CCFF"><formname="form1"method="get"action="index.php"><tr><tdheight=35align="center">請輸入用戶編號<inputname="Action"type="text"id="Action"><inputtype="submit"name="Submit"value="提交"></td></tr></form><?php if($Submit==true){ $HawkXML=newMessage_XML; $HawkXML->select_message($_GET["Action"]); } ?></table></body></html>message.xml:<?xmlversion="1.0"encoding="GB2312"?><message><record><admin_id>2007424065018</admin_id><user>英特爾</user><pass>123456</pass><address>長春市</address></record><record><admin_id>2007424065056</admin_id><user>網(wǎng)易論壇</user><pass>789465</pass><address>長春市</address></record><record><admin_id>2007424065133</admin_id><user>網(wǎng)易軟件</user><pass>456456</pass><address>長春市</address></record><record><admin_id>2007426050235</admin_id><user>mrsoft</user><pass>123456</pass><address>長春市</address></record></message>五修改XML文件index.php:<?php classMessage_XMLextendsDomDocument{ //屬性 private$Root; //方法 //構(gòu)造函數(shù) publicfunction__construct(){ parent::__construct(); //創(chuàng)建或讀取存儲(chǔ)留言信息的XML文檔message.xml $this->load("message.xml");}publicfunctionshow_message(){$root=$this-documentElement; $xpath=newDOMXPath($this); $Node_Record=$this->getElementsByTagName("record"); $Node_Record_length=$Node_Record->length; print"<tablewidth='450'bgcolor='#97F7ED'><tr>";print"<tdwidth='112'height='22'align='center'>"; print"<b>用戶名</b>"; print"</td><tdwidth='112'align='center'>";print"<b>密碼</b>"; print"</td><tdwidth='113'align='center'>"; print"<b>地址</b></td><tdwidth='113'>"; print" "; print"</td></tr>"; for($i=0;$i<$Node_Record->length;$i++){ $k=0; foreach($Node_Record->item($i)->childNodesas$articles){ $field[$k]=iconv("UTF-8","GB2312",$articles->textContent); $k++; } print"<tablewidth='450'bgcolor='#97F7ED'><tr>"; print"<tdwidth='112'height='22'align='center'>"; print"$field[1]"; print"</td><tdwidth='112'align='center'>";print"$field[2]"; print"</td><tdwidth='113'align='center'>"; print"$field[3]"; print"</td><tdwidth='113'align='center'>"; print"<ahref='?Action=update_message&admin_id=$field[0]'>修改</a></td>"; print"</tr></table>"; }}//修改數(shù)據(jù)publicfunctionupdate_message($admin_id){ $Root=$this->documentElement; $xpath=newDOMXPath($this); $Node_Record=$xpath->query("http://record[admin_id='$admin_id']"); $g=0; foreach($Node_Record->item(0)->childNodesas$articles){ $field[$g]=iconv("UTF-8","GB2312",$articles->textContent); $g++; } print"<tablewidth='450'bgcolor='#97F7ED'><formmethod='post'action='?Action=save_message&admin_id=$admin_id'>"; print"<tr><tdwidth='95'align='right'>用戶名:</td><td><inputtype=textname='user'value='$field[1]'size=30></td></tr>"; print"<tr><tdwidth='95'align='right'>密碼:</td><td><inputtype=textname='pass'value='$field[2]'size=30></td></tr>"; print"<tr><tdwidth='95'align='right'>地址:</td><td><inputtype=textname='address'value='$field[3]'size=30></td></tr>"; print"<tr><tdcolspan='2'align='center'><inputtype='submit'value='修改數(shù)據(jù)'></td></tr></form></table>";} //保存數(shù)據(jù)publicfunctionsave_message($admin_id,$user,$pass,$address) { $Root=$this->documentElement; //查詢待修改的記錄 $xpath=newDOMXPath($this); $Node_Record=$xpath->query("http://record[admin_id='$admin_id']"); $Replace[0]=$admin_id; $Replace[1]=$user; $Replace[2]=$pass; $Replace[3]=$address; $d=0; //修改 foreach($Node_Record->item(0)->childNodesas$articles){ $Node_newText=$this->createTextNode(iconv("GB2312","UTF-8",$Replace[$d])); $articles->replaceChild($Node_newText,$articles->lastChild); $d++; } echo"<script>alert('修改成功');location.href='".$_SERVER['PHP_SELF']."'</script>"; $this->save("message.xml"); }}?><html><head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><title>修改XML文件</title><styletype="text/css"><!--body,td,th{ font-size:13px;}a:link{ text-decoration:none;}a:visited{ text-decoration:none;}a:hover{ text-decoration:none;}a:active{ text-decoration:none;}.STYLE2{font-size:18px}--></style></head><body><tablewidth="450"height="50"border="0"cellpadding="0"cellspacing="0"><tr><tdheight="50"align="center"bgcolor="#97F7ED"><ahref=?Action=show_messageclass="STYLE2">修改XML文件</a></td></tr><?php $hawkXML=newMessage_XML; $Action=""; if(isset($_GET['Action'])) $Action=$_GET['Action']; switch($Action){case"show_message": $hawkXML->show_message(); break; case"update_message"://修改 $hawkXML->update_message($_GET['admin_id']); break; case"save_message"://保存 $hawkXML->save_sage($_GET['admin_id'],$_POST['user'],$_POST['pass'],$_POST['address']); break;}?></table></body></html>message.xml:<?xmlversion="1.0"encoding="GB2312"?><message><record><admin_id>2007424065018</admin_id><user>英特爾</user><pass>123456</pass><address>長春市</address></record><record><admin_id>2007424065056</admin_id><user>網(wǎng)易論壇</user><pass>789465</pass><address>長春市</address></record><record><admin_id>2007424065133</admin_id><user>網(wǎng)易軟件</user><pass>456456</pass><address>長春市</address></record><record><admin_id>2007426050235</admin_id><user>mrsoft</user><pass>123456</pass><address>長春市</address></record></message>六刪除XML文件index.php:<?php classMessage_XMLextendsDomDocument{ private$Root; publicfunction__construct(){ parent::__construct(); //創(chuàng)建或讀取存儲(chǔ)留言信息的XML文檔message.xml $this->load("message.xml");}publicfunctiondelete_message($admin_id){ $Root=$this->documentElement; $xpath=newDOMXPath($this); $Node_Record=$xpath->query("http://record[admin_id='$admin_id']"); $Root->removeChild($Node_Record->item(0)); $this->save("message.xml"); echo"<script>alert('刪除成功');location.href='".$_SERVER['PHP_SELF']."'</script>";}publicfunctionshow_message() { $Root=$this->documentElement; $xpath=newDOMXPath($this); $Node_Record=$this->getElementsByTagName("record"); $Node_Record_Length=$Node_Record->length; print"<tablewidth='450'bgcolor='#97F7ED'><tr>";print"<tdwidth='112'height='22'align='center'>"; print"<b>用戶名</b>"; print"</td><tdwidth='112'align='center'>";print"<b>密碼</b>"; print"</td><tdwidth='113'align='center'>"; print"<b>地址</b></td><tdwidth='113'>"; print" "; print"</td></tr>"; for($i=0;$i<$Node_Record->length;$i++) { $K=0; foreach($Node_Record->item($i)->childNodesas$articles){ $Field[$K]=iconv("UTF-8","GB2312",$articles->textContent); $K++;} print"<tablewidth='450'bgcolor='#97F7ED'><tr>"; print"<tdwidth='112'height='22'align='center'>"; print"$Field[1]"; print"</td><tdwidth='112'align='center'>";print"$Field[2]"; print"</td><tdwidth='113'align='center'>"; print"$Field[3]"; print"</td><tdwidth='113'align='center'>"; print"<ahref='?Action=delete_message&admin_id=$Field[0]'>刪除</a></td>"; print"</tr></table>"; }}}?><html><head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><title>刪除XML文件</title><styletype="text/css"><!--body,td,th{ font-size:13px;}a:link{ text-decoration:none;}a:visited{ text-decoration:none;}a:hover{ text-decoration:none;}a:active{ text-decoration:none;}.STYLE2{font-size:18px}--></style></head><body><tablewidth="450"height="50"border="0"cellpadding="0"cellspacing="0"><tr><tdheight="50"align="center"bgcolor="#97F7ED"><ahref=?Action=show_messageclass="STYLE2">刪除XML文件</a></td></tr><?php$HawkXML=newMessage_XML; $Action=""; if(isset($_GET['Action'])) $Action=$_GET['Action']; switch($Action){ case"show_message": $HawkXML->show_message

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論