PHP如何封装XML简单操作类
创始人
2024-12-08 03:22:24
0次

具体如下:

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) "详细内容三"
  }
 }
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...