自定义php类(查找/修改)xml文档


Posted in PHP onMarch 26, 2013

近期在看PHP的教学视频,其中讲到了 PHP 操作 xml 文档,学了点儿 DOMDocument 类。自己查手册又全英文,看不大懂。但还是自己写了个类,实现了查找 xml 节点,并修改节点值。背景解说完毕,且看代码如下:

/* 
<?xml version="1.0" encoding="UTF-8"?> 
<班级> 
<学生 number="101"> 
<名字>孙悟空</名字> 
<名字>孙行者</名字> 
<年龄>猴精猴精</年龄> 
<介绍></介绍> 
</学生> 
<学生 number="102"> 
<名字>白骨精</名字> 
<年龄>140</年龄> 
<介绍>幻化万千</介绍> 
</学生> 
<学生 number="103"> 
<名字>猪八戒</名字> 
<名字>猪无能</名字> 
<年龄>200</年龄> 
<介绍>能吃会睡</介绍> 
</学生> 
</班级> 
*/ 
class xmlDom{ 
public $version; 
public $encoding; 
private $xml; 
private $items; 
private $seachNode = ''; 
private $seachItem = ''; 
private $seachValue = ''; 
public $writeBytes = 0; 
function __construct($xmlFile ='', $version ='1.0', $encoding = 'UTF-8'){ 
$this->version = $version; 
$this->encoding = $encoding; 
$this->xml = new DOMDocument($version, $encoding); 
if($xmlFile)$this->xml->load($xmlFile); 
} 
function getRootEle($rootTag){ 
$this->xmlRoot = $this->xml->getElementsByTagName($rootTag)->item(0); 
} 
function getSeachItem($itemsTag, $seachNode, $seachValue){ 
$this->items = $this->xml->getElementsByTagName($itemsTag); 
$this->items->length; 
for($i=0; $i<$this->items->length; $i++){ 
$item = $this->items->item($i);//元素 
$node = $item->getElementsByTagName($seachNode);//节点 
for($j = 0; $j< $node->length; $j++){ 
$subNode = $node->item($j); 
if($seachValue == $subNode->nodeValue){ 
$this->seachNode = $subNode; 
$this->seachItem = $item; 
$this->seachValue = $subNode->nodeValue; 
break(2); 
} 
} 
} 
return ($this->seachNode) ? true : false; 
} 
function update($nodeValue, $nodeTag = '',$append = false, $index = 0){ 
if($append){ 
if($nodeTag) 
$this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue += $nodeValue; 
else 
$this->seachNode->nodeValue += $nodeValue; 
}else{ 
if($nodeTag) 
$this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue = $nodeValue; 
else 
$this->seachNode->nodeValue = $nodeValue; 
} 
} 
function save($filename){ 
$this->writeBytes = $this->xml->save($filename); 
return ($this->writeBytes) ? true : false; 
} 
} 
$test = new xmlDom('student.xml'); 
$test->getSeachItem('学生','年龄','103');//找到 年龄=103 的猪八戒 
$test->update('小猪猪', '名字', false, 1); //把猪八戒的第二个名字改成:小猪猪 
$test->save('new.xml'); //保存成新文件
PHP 相关文章推荐
基于mysql的论坛(1)
Oct 09 PHP
php+iframe实现隐藏无刷新上传文件
Feb 10 PHP
PHP JS Ip地址及域名格式检测代码
Sep 27 PHP
php中实现进程锁与多进程的方法
Sep 18 PHP
PHP关键特性之命名空间实例详解
May 06 PHP
Joomla框架实现字符串截取的方法示例
Jul 18 PHP
PHP实现的回溯算法示例
Aug 15 PHP
使用YII2框架实现微信公众号中表单提交功能
Sep 04 PHP
ThinkPHP实现转换数据库查询结果数据到对应类型的方法
Nov 16 PHP
PHP通过调用新浪API生成t.cn格式短网址链接的方法详解
Feb 20 PHP
PHP单文件上传原理及上传函数的封装操作示例
Sep 02 PHP
php 中self,this的区别和操作方法实例分析
Nov 04 PHP
php中DOMElement操作xml文档实例演示
Mar 26 #PHP
PHP 自定义错误处理函数trigger_error()
Mar 26 #PHP
PHP中图片等比缩放的实例
Mar 24 #PHP
比较简单的百度网盘文件直链PHP代码
Mar 24 #PHP
php实现单链表的实例代码
Mar 22 #PHP
php 判断数组是几维数组
Mar 20 #PHP
php页面消耗内存过大的处理办法
Mar 18 #PHP
You might like
Trying to clone an uncloneable object of class Imagic的解决方法
2012/01/11 PHP
PHP解决URL中文GBK乱码问题的两种方法
2014/06/03 PHP
使用php方法curl抓取AJAX异步内容思路分析及代码分享
2014/08/25 PHP
朋友网关于QQ相关的PHP代码(研究QQ的绝佳资料)
2015/01/26 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
js的写法基础分析
2011/01/17 Javascript
jQuery 1.5 源码解读 面向中高阶JSER
2011/04/05 Javascript
jquery打开直接跳到网页最下面、最低端实现代码
2013/04/22 Javascript
实现51Map地图接口(示例代码)
2013/11/22 Javascript
JavaScript实现的一个倒计时的类
2015/03/12 Javascript
基于jquery实现的树形菜单效果代码
2015/09/06 Javascript
jQuery插件开发精品教程(让你的jQuery更上一个台阶)
2015/11/07 Javascript
Node.js中 __dirname 的使用介绍
2017/06/19 Javascript
JS中定位 position 的使用实例代码
2017/08/06 Javascript
vue路由懒加载的实现方法
2018/03/12 Javascript
vue.js使用3DES加密的方法示例
2018/05/18 Javascript
vue实现日历备忘录功能
2020/09/24 Javascript
解决$store.getters调用不执行的问题
2019/11/08 Javascript
原生js实现文件上传、下载、封装等实例方法
2020/01/05 Javascript
python实现向ppt文件里插入新幻灯片页面的方法
2015/04/28 Python
pandas按若干个列的组合条件筛选数据的方法
2018/04/11 Python
Python 修改列表中的元素方法
2018/06/26 Python
python银行系统实现源码
2019/10/25 Python
python如何获取apk的packagename和activity
2020/01/10 Python
python程序输出无内容的解决方式
2020/04/09 Python
python读取excel数据绘制简单曲线图的完整步骤记录
2020/10/30 Python
python 利用matplotlib在3D空间绘制二次抛物面的案例
2021/02/06 Python
支持IE8的纯css3开发的响应式设计动画菜单教程
2014/11/05 HTML / CSS
Blue Nile台湾:钻石珠宝商,订婚首饰、结婚戒指和精品首饰
2017/11/24 全球购物
《二泉映月》教学反思
2014/04/15 职场文书
员工廉洁自律承诺书
2014/05/26 职场文书
党员四风自我剖析材料思想汇报
2014/09/13 职场文书
党的群众路线教育实践活动实施方案
2014/10/31 职场文书
英文慰问信范文
2015/03/24 职场文书
退休职工欢送会致辞
2015/08/01 职场文书
2019幼儿教师求职信(3篇)
2019/09/20 职场文书