栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

本文实例讲述了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);
    }
  }
  
  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;
  }
  
  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.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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/40143.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号