phpxml操作类(自定义)
时间:2021-07-01 10:21:17
帮助过:7人阅读
/** - php xml操作类
- link:http://bbs.it-home.org
- */
- 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('');
- 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文件,参考格式如下:
-
- 1
-
- peng.zhou
- zxcxzcxzczxcxz
- cxzcxzc
-
-
- add_group
- cn_group
- 1
-
-
- 2
-
- peng.zhou
- sales
-
-
-
- add_member
- mail_group
- 1
-
|