侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP封装的XML简单操作类完整实例

php  /  管理员 发布于 7年前   188

本文实例讲述了PHP封装的XML简单操作类。分享给大家供大家参考,具体如下:

xml_dom.php封装类文件:

dbfile = $db_file;    if(!file_exists($db_file))    {//     die('未找到数据库文件');      $this->dblink = new DOMDocument('1.0', 'utf-8');      $root = $this->dblink->createElement('root');      $this->dblink->appendChild($root);      $this->dblink->formatOutput = true;  // xml文件保留缩进样式      $this->dblink->save($this->dbfile);    }    else    {      $this->dblink = new DOMDocument();      $this->dblink->formatOutput = true;      $this->dblink->load($this->dbfile);    }  }  /**   * 遍历所有元素   * ===============================================   * 标准xml文件,一个元素可能有n个属性,可用自定义键[nodevalue]获取元素值   *    *    *   1   *   标题一   *   详细内容一   * 
* =============================================== * 简单xml文件,没有属性,键值一一对应 * * * * 1 * 标题一 * 详细内容一 * * * @param $node * @return array */ function getData($node=0){ if(!$node) { $node = $this->dblink->documentElement; } $array = array(); foreach($node->attributes as $attribute) { $key = $attribute->nodeName; $val = $attribute->nodeValue; $array[$key] = $val; } if(count($array)) // 有属性,则用[nodevalue]键代表值 { $array['nodevalue'] = $node->nodeValue; } // 递归遍历所有子元素 $node_child = $node->firstChild; while($node_child) { if(XML_ELEMENT_NODE == $node_child->nodeType) { $tagname = $node_child->tagName; $result = $this->getData($node_child); if(isset($array[$tagname])) // 发现有重复tagName的子元素存在,所以改用数组存储重复tagName的所有子元素 { if(!is_array($array[$tagname][0])) {$tmp = $array[$tagname];$array[$tagname] = array();$array[$tagname][] = $tmp; } $array[$tagname][] = $result; } else { $array[$tagname] = $result; } } $node_child = $node_child->nextSibling; } if(!count($array)) // 没有子元素&没有属性=最末子元素,就返回该元素的nodeValue值 { return $node->nodeValue; } return $array; } /** * 把array数据写到xml文件(覆盖) * @param $data */ public function setData($data,&$node=0) { $is_root = false; if(!$node) { $is_root = true; $node = $this->dblink->documentElement; // 清除原数据 $remove = array(); $node_child = $node->firstChild; while($node_child) { $remove[] = $node_child; $node_child = $node_child->nextSibling; } foreach($remove as $r) { $node->removeChild($r); } } if(is_array($data)) { foreach($data as $k=>$v) { if(is_array($v)) { foreach($v as $r) {$item = $this->dblink->createElement($k);$result = $this->setData($r,$item);$node->appendChild($result); } } else { $item = $this->dblink->createElement($k); $value = $this->dblink->createTextNode($v); $item->appendChild($value); $node->appendChild($item); } } } else { $item = $this->dblink->createTextNode($data); $node->appendChild($item); } if($is_root) { $this->dblink->save($this->dbfile); // 覆盖写入 } else { return $node; } }}

简单用法示例如下:

smp.xml文件:

        1     标题一     详细内容一           2     标题二     详细内容二           3     标题三     详细内容三   

index.php文件:

include("xml_dom.php");$xml=new xml_dom("smp.xml");//载入xml文件$xmlarr=$xml->getData();//读取xml文件内容var_dump($xmlarr);

运行结果:

array(1) { ["posts"]=> array(3) {  [0]=>  array(3) {   ["id"]=>   string(1) "1"   ["title"]=>   string(9) "标题一"   ["content"]=>   string(15) "详细内容一"  }  [1]=>  array(3) {   ["id"]=>   string(1) "2"   ["title"]=>   string(9) "标题二"   ["content"]=>   string(15) "详细内容二"  }  [2]=>  array(3) {   ["id"]=>   string(1) "3"   ["title"]=>   string(9) "标题三"   ["content"]=>   string(15) "详细内容三"  } }}

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools..net.cn/code/xmljson

在线格式化XML/在线压缩XML:
http://tools..net.cn/code/xmlformat

XML在线压缩/格式化工具:
http://tools..net.cn/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools..net.cn/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php+Ajax处理xml与json格式数据的方法示例
  • PHP以json或xml格式返回请求数据的方法
  • php实现xml与json之间的相互转换功能实例
  • PHP生成json和xml类型接口数据格式
  • PHP实现返回JSON和XML的类分享
  • php json与xml序列化/反序列化
  • php 备份数据库代码(生成word,excel,json,xml,sql)
  • PHP数组生成XML格式数据的封装类实例
  • php封装json通信接口详解及实例
  • PHP封装返回Ajax字符串和JSON数组的方法
  • PHP封装XML和JSON格式数据接口操作示例


  • 上一条:
    PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】
    下一条:
    PHP开发中解决并发问题的几种实现方法分析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客