《PHP教程:PHP實現對xml的增刪改查操作案例分析》要點:
本文介紹了PHP教程:PHP實現對xml的增刪改查操作案例分析,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了PHP實現對xml的增刪改查操作.分享給大家供大家參考,具體如下:PHP實例
案例:PHP實例
index.phpPHP實例
<?php header("content-type:text/html;charset=utf-8"); $xmldom = new DOMDocument(); $xmldom->load("demo2.xml"); //查詢學生信息 $stus = $xmldom->getElementsByTagName("學生"); for ($i=0;$i<$stus->length;$i++){ $stu = $stus->item($i); getxmlnode($stu, "姓名"); getxmlnode($stu, "年齡"); getxmlnode($stu, "性別"); getxmlnode($stu, "介紹"); } function getxmlnode(&$stu,$tagname){ echo $stuname = $stu->getElementsByTagName($tagname)->item(0)->nodeValue."<br/>"; } //添加一個學生信息 //addxml($xmldom); function addxml($xmldom){ $root = $xmldom->getElementsByTagName("班級")->item(0); $ostus = $xmldom->createElement_x_x("學生"); //添加屬性 $ostus->setAttribute("戀愛狀況","熱戀中"); //$ostus->nodeValue="\r\n"; $root->a($ostus); $ostu_name = $xmldom->createElement_x_x("姓名"); $ostus->a($ostu_name); $ostu_name->nodeValue="小娜"; $ostu_sex = $xmldom->createElement_x_x("性別"); $ostus->a($ostu_sex); $ostu_sex->nodeValue="女"; $ostu_age = $xmldom->createElement_x_x("年齡"); $ostus->a($ostu_age); $ostu_age->nodeValue="23"; $ostu_intro = $xmldom->createElement_x_x("介紹"); $ostus->a($ostu_intro); $ostu_intro->nodeValue="高一美女"; $xmldom->save("demo2.xml"); } //刪除一個學生信息 //del_element($xmldom); function del_element($xmldom){ $dstus = $xmldom->getElementsByTagName("學生"); $laststu = $dstus->item($dstus->length-1); $laststu->parentNode->removeChild($laststu); } //修改一個學生信息 //update_element($xmldom); function update_element($xmldom){ $ustus = $xmldom->getElementsByTagName("學生"); $ustu = $ustus->item(0); $ustu_age = $ustu->getElementsByTagName("年齡")->item(0); $ustu_age->nodeValue+=10; } //寫會到文件中 $xmldom->save("demo2.xml"); ?>
demo2.xmlPHP實例
<?xml version="1.0" encoding="UTF-8"?> <班級> </班級>
PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:PHP實例
在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljsonPHP實例
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformatPHP實例
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compressPHP實例
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformatPHP實例
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP實例
希望本文所述對大家PHP程序設計有所幫助.PHP實例
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/782.html