php xml操作类(自定义)

发布时间:2020-02-13编辑:脚本学堂
在php开发中,经常会用到XML文件作为配置文件,或作为轻量级的数据库,还是挺好用的。只要XML文件大小小于2M,在php下的读取速度还是不错的。

在php开发中,经常会用到XML文件作为配置文件,或作为轻量级的数据库,还是挺好用的。
只要XML文件大小小于2M,在php下的读取速度还是不错的。

这里为大家提供一个自己写的php xml操作类,仅供大家学习参考。

如果需要循环利用XML作为数据库,只要记录节点总数,当到达一个饱和数量时,再重新初始化XML即可。

1、xml操作类
 

复制代码 代码示例:

<?php
/**
 php xml操作类
 link:http://www.jb200.com
*/
class requirelog
{
    protected static $requirelog;

    //get ready for xml,if xml is not found ,create xml
    static function _initializeLog()
    {
       if(file_exists('requirelog.xml'))
       {

           self::$requirelog = DOMDocument::load('requirelog.xml');
       }
       else
       {
           self::$requirelog = new DOMDocument('1.0', 'iso-8859-1');
           self::$requirelog->loadXML('<?xml version="1.0" encoding="UTF-8"?><contents></contents>');
           self::$requirelog->save('requirelog.xml');
       }
    }
    //获取finish字段全部为0的所有节点
    public static function get_entrys()
    {
            self::_initializeLog();
            $entrys = self::$requirelog->getElementsByTagName('entry');
            $entry_arrays = array();
            foreach($entrys as $entry)
            {
                if($entry->getElementsByTagName('finish')->item(0)->nodeValue == 0)
                {
                    $entry_array = array();
                    $entry_array['id'] = $entry->getElementsByTagName('id')->item(0)->nodeValue;
                    $entry_array['time'] = $entry->getElementsByTagName('time')->item(0)->nodeValue;
                    $entry_array['operator'] = $entry->getElementsByTagName('operator')->item(0)->nodeValue;
                    $entry_array['byoperator'] = $entry->getElementsByTagName('byoperator')->item(0)->nodeValue;
                    $entry_array['parameters1'] = $entry->getElementsByTagName('parameters1')->item(0)->nodeValue;
                    $entry_array['parameters2'] = $entry->getElementsByTagName('parameters2')->item(0)->nodeValue;
                    $entry_array['parameters3'] = $entry->getElementsByTagName('parameters3')->item(0)->nodeValue;

                    $entry_array['type'] = $entry->getElementsByTagName('type')->item(0)->nodeValue;
                    $entry_array['module'] = $entry->getElementsByTagName('module')->item(0)->nodeValue;
                    $entry_array['finish'] = $entry->getElementsByTagName('finish')->item(0)->nodeValue;
                    $entry_arrays[] = $entry_array;
                }
            }
            return  $entry_arrays;
    }
   
    //获取所有的节点
    public static function get_count_entrys()
    {
            self::_initializeLog();
            $entrys = self::$requirelog->getElementsByTagName('entry');
            $entry_arrays = array();
            foreach($entrys as $entry)
            {

                $entry_array = array();
                $entry_array['id'] = $entry->getElementsByTagName('id')->item(0)->nodeValue;
                $entry_array['time'] = $entry->getElementsByTagName('time')->item(0)->nodeValue;
                $entry_array['operator'] = $entry->getElementsByTagName('operator')->item(0)->nodeValue;
                $entry_array['byoperator'] = $entry->getElementsByTagName('byoperator')->item(0)->nodeValue;
                $entry_array['parameters1'] = $entry->getElementsByTagName('parameters1')->item(0)->nodeValue;
                $entry_array['parameters2'] = $entry->getElementsByTagName('parameters2')->item(0)->nodeValue;
                $entry_array['parameters3'] = $entry->getElementsByTagName('parameters3')->item(0)->nodeValue;

                $entry_array['type'] = $entry->getElementsByTagName('type')->item(0)->nodeValue;
                $entry_array['module'] = $entry->getElementsByTagName('module')->item(0)->nodeValue;
                $entry_array['finish'] = $entry->getElementsByTagName('finish')->item(0)->nodeValue;
                $entry_arrays[] = $entry_array;

            }
            return  $entry_arrays;
    }

    //根据ID获取节点
    public static function get_entry($id)
    {
        self::_initializeLog();
        $count=count(self::get_count_entrys());
        if($count > 0)
        {
            $entrys = self::$requirelog->getElementsByTagName('entry');
            foreach($entrys as $entry)
            {
                if ($id == $entry->getElementsByTagName('id')->item(0)->nodeValue)
                {
                    $entry_array = array();
                    $entry_array['id'] = $entry->getElementsByTagName('id')->item(0)->nodeValue;
                    $entry_array['time'] = $entry->getElementsByTagName('time')->item(0)->nodeValue;
                    $entry_array['operator'] = $entry->getElementsByTagName('operator')->item(0)->nodeValue;
                    $entry_array['byoperator'] = $entry->getElementsByTagName('byoperator')->item(0)->nodeValue;
                    $entry_array['parameters1'] = $entry->getElementsByTagName('parameters1')->item(0)->nodeValue;
                    $entry_array['parameters2'] = $entry->getElementsByTagName('parameters2')->item(0)->nodeValue;
                    $entry_array['parameters3'] = $entry->getElementsByTagName('parameters3')->item(0)->nodeValue;

                    $entry_array['type'] = $entry->getElementsByTagName('type')->item(0)->nodeValue;
                    $entry_array['module'] = $entry->getElementsByTagName('module')->item(0)->nodeValue;
                    $entry_array['finish'] = $entry->getElementsByTagName('finish')->item(0)->nodeValue;
                    return $entry_array;
                }
            }
        }
        else
            return null;
    }

    //执行完用户请求,更改finsh字段
    public static function do_finsh($id)
    {
        self::_initializeLog();
        $entrys = self::$requirelog->getElementsByTagName('entry');
        $entry_arrays = array();
        foreach($entrys as $entry)
        {
            if($entry->getElementsByTagName('id')->item(0)->nodeValue == $id)
            {
                $entry->getElementsByTagName('finish')->item(0)->nodeValue = 1;
            }
        }

        self::$requirelog->save('requirelog.xml');
    }

    //增加新节点,$operator为操作者,$byoperator是被操作对象,$parameters1,2,3为可扩展的参数,$type为类型,$module为影响的模块
   static function add_entry($operator,$byoperator,$parameters1,$parameters2,$parameters3,$type,$module)
    {
        //$contents = self::$restlog->getElementsByTagName('contents');

        self::_initializeLog();
        $count=count(self::get_count_entrys());

        $entry = self::$requirelog->createElement('entry');

        $id = self::$requirelog->createElement('id');
        $idvalue = self::$requirelog->createTextNode($count+1);
        $id->appendChild($idvalue);

        $time = self::$requirelog->createElement('time');
        $timevalue = self::$requirelog->createTextNode(date("Y-m-d H:i:s",time()));
        $time->appendChild($timevalue);

        $op = self::$requirelog->createElement('operator');
        $opvalue = self::$requirelog->createTextNode($operator);
        $op->appendChild($opvalue);

        $bop = self::$requirelog->createElement('byoperator');
        $bopvalue = self::$requirelog->createTextNode($byoperator);
        $bop->appendChild($bopvalue);

        $ps1 = self::$requirelog->createElement('parameters1');
        $ps1value = self::$requirelog->createTextNode($parameters1);
        $ps1->appendChild($ps1value);

        $ps2 = self::$requirelog->createElement('parameters2');
        $ps2value = self::$requirelog->createTextNode($parameters2);
        $ps2->appendChild($ps2value);

        $ps3 = self::$requirelog->createElement('parameters3');
        $ps3value = self::$requirelog->createTextNode($parameters3);
        $ps3->appendChild($ps3value);


        $tp = self::$requirelog->createElement('type');
        $tpvalue = self::$requirelog->createTextNode($type);
        $tp->appendChild($tpvalue);

        $md = self::$requirelog->createElement('module');
        $mdvalue = self::$requirelog->createTextNode($module);
        $md->appendChild($mdvalue);

        $fi = self::$requirelog->createElement('finish');
        $fivalue = self::$requirelog->createTextNode(0);
        $fi->appendChild($fivalue);

        $entry->appendChild($id);
        $entry->appendChild($time);
        $entry->appendChild($op);
        $entry->appendChild($bop);
        $entry->appendChild($ps1);
        $entry->appendChild($ps2);
        $entry->appendChild($ps3);
        $entry->appendChild($tp);
        $entry->appendChild($md);
        $entry->appendChild($fi);

        self::$requirelog->documentElement->appendChild($entry);
        self::$requirelog->save('requirelog.xml');

        return 1;
    }
}
?>

2、requirelog.xml文件,参考格式如下:
 

复制代码 代码示例:
<?xml version="1.0" encoding="UTF-8"?>
<contents>
   <entry>
      <id>1</id>
      <time>2009-11-21 16:56:33</time>
      <operator>peng.zhou</operator>
      <byoperator>zxcxzcxzczxcxz</byoperator>
      <parameters1>cxzcxzc</parameters1>
      <parameters2/>
      <parameters3/>
      <type>add_group</type>
      <module>cn_group</module>
      <finish>1</finish>
   </entry>
   <entry>
      <id>2</id>
      <time>2009-11-21 16:56:53</time>
      <operator>peng.zhou</operator>
      <byoperator>sales</byoperator>
      <parameters1/>
      <parameters2/>
      <parameters3/>
      <type>add_member</type>
      <module>mail_group</module>
      <finish>1</finish>
   </entry>
</contents>