加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php里操作xml文档程序代码

发布时间:2022-02-10 13:05:53 所属栏目:PHP教程 来源:互联网
导读:下面我先是介绍一个朋友自己写的一个操作xml文档程序,然后再介绍了php中一个自带的解析xml文档的函数功能,有需要的朋友可参考,代码如下: /* ?xml version=1.0 encoding=UTF-8? 班级 学生 number=101 名字孙悟空/名字 名字孙行者/名字 年龄猴精猴精/年龄 介
  下面我先是介绍一个朋友自己写的一个操作xml文档程序,然后再介绍了php中一个自带的解析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;
      }
  }
  //开源代码Cuoxin.com
  $test = new xmlDom('student.xml');
  $test->getSeachItem('学生','年龄','103');//找到 年龄=103 的猪八戒
  $test->update('小猪猪', '名字', false, 1); //把猪八戒的第二个名字改成:小猪猪
  $test->save('new.xml'); //保存成新文件
  上面是使用了dom来操作,下面我们利用php中的SimpleXML来操作xml,也算是很标准的一个操作xml文档的类了.

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读