PHP實現(xiàn)對xml進行簡單的增刪改查(CRUD)操作示例
本文實例講述了PHP實現(xiàn)對xml進行簡單的增刪改查(CRUD)操作。分享給大家供大家參考,具體如下:
假如有下面xml文件:
<?xml version="1.0" encoding="UTF-8"?> <setting> <preferTime>55.8</preferTime> <playerValue>56</playerValue> <reduceValue>40</reduceValue> <reduceTime>339</reduceTime> </setting>
如何使用php對它進行CRUD?其實像這種簡單的xml文件使用SimpleXMl再好不過了。你可以像這樣來操作它:
<?php
//獲取數(shù)據(jù) get the config data
if(isset($_GET["type"])){
if($_GET["type"]=="get"){
$xml=simplexml_load_file("../config.xml");
$config=array("preferTime"=>$xml->preferTime."",
"playerValue"=>$xml->playerValue."",
"reduceValue"=>$xml->reduceValue."",
"reduceTime"=>$xml->reduceTime."");
echo json_encode($config);
}
//更新數(shù)據(jù) update the config data
if($_GET["type"]=="update"){
$xml=simplexml_load_file("../config.xml");
$xml->preferTime=$_GET["data"]["preferTime"];
$xml->playerValue=$_GET["data"]["playerValue"];
$xml->reduceValue=$_GET["data"]["reduceValue"];
$xml->reduceTime=$_GET["data"]["reduceTime"];
$xml->asXML("../config.xml");
echo json_encode("save success!");
}
}
更多詳情可參考PHP官方usage examples 和 API description .
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯誤與異常處理方法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP正則表達式處理函數(shù)(PCRE 函數(shù))實例小結(jié)
這篇文章主要介紹了PHP正則表達式處理函數(shù)(PCRE 函數(shù)),結(jié)合實例形式總結(jié)分析了php正則表達式preg_replace、preg_match、preg_match_all、preg_split及preg_quote等函數(shù)相關(guān)使用技巧,需要的朋友可以參考下2019-05-05
php遇到錯誤Call to undefined function ImageCreate()解決方法
剛配置好服務器,運行php的時候提示Call to undefined function imagecreate錯誤,經(jīng)過百度發(fā)現(xiàn)是php不支持gd庫,linux服務器需要重新make,windows下比較簡單了,下面是具體的方法2021-09-09
DW中鏈接mysql數(shù)據(jù)庫時,建立字符集中文出現(xiàn)亂碼的解決方法
DW中鏈接mysql數(shù)據(jù)庫時,建立字符集中文出現(xiàn)亂碼“????”2010-03-03

