// Configure //-------------------------------------------------------------------------------------
The code is as follows: |
Copy code |
Function loadfile ($ file ){ $ Newfile = new domdocument (); $ Newfile-> validateonparse = true; $ Newfile-> load ($ file ); Return $ newfile; } Function add ($ file, $ parentname, $ children) {// add an xml node $ 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) {// delete an xml node $ 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) {// edit an xml node $ 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) {// move an xml node $ 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 ); } |