//-------------------------------------------------------------------------------------
代码如下 |
复制代码 |
function loadfile($file){
$newfile=new domdocument();
$newfile->validateonparse=true;
$newfile->load($file);
return $newfile;
}
function add($file, $parentname, $children){ //增加xml节点
$xml=loadfile($file);
$id=uniqid('m' . rand(1,5), true);
$parentnode=$xml->createelement($parentname);
$parentnode->setattribute('mid', $id);
foreach($children as $child => $value){
$childnode=$xml->createelement($child, $value);
$parentnode->appendchild($childnode);
}
$xml->documentelement->appendchild($parentnode);
$xml->save($file);
return $id;
}
function delete($file, $id){//删除xml 节点
$xml=loadfile($file);
$ids=explode(",", $id);
foreach ($ids as $oldnodeid){
$oldnode=$xml->getelementbyid($oldnodeid);
$parentnode=$oldnode->parentnode;
$parentnode->removechild($oldnode);
}
$xml->save($file);
}
function edit($file, $id, $child, $value){//编辑xml 节点
$xml=loadfile($file);
$parentnode=$xml->getelementbyid($id);
$childnode=$parentnode->childnodes->item($child);
$textnode=$childnode->childnodes->item(0);
$textnode->nodevalue=$value;
$xml->save($file);
}
function move($file, $moveid, $refid=null){ //移动xml节点
$xml=loadfile($file);
$movenode=$xml->getelementbyid($moveid);
$parentnode=$movenode->parentnode;
if ($refid!=null) {
$refnode=$xml->getelementbyid($refid);
if(!$parentnode->issamenode($refnode->parentnode)) return false;
}
else $refnode=null;
$movenode=$parentnode->removechild($movenode);
$parentnode->insertbefore($movenode,$refnode);
$xml->save($file);
}
|