This tutorial uses the phpdomdocument function to modify, add, edit, and delete xml nodes. each operation node under the code is described in English, if you can write a program, I think this can be understood in English. modify, add, edit... this tutorial uses the php domdocument function to modify, add, edit, and delete xml nodes. each operation node under the code is described in English, if you can write a program, I think this can be understood in English.
Modify, add, edit, and delete php xml nodes as follows:
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 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 );}
Article address:
Reprint at will ^ please include the address of this article!