本教程是利用了php domdocument函数来对xml节点 修改,增加,编辑,删除代码下面每个操作节点都是英文说明,如果你能写程序我想这些英文都能看得懂的.
php xml节点 修改,增加,编辑,删除代码如下:
<?php 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); }
文章地址:
转载随意^^请带上本文地址!