php xml 封装类,php xml操作类(自定义)

/**

php xml操作类

link:http://www.jquerycn.cn

*/

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"?>');

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;

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值