Modify, add, edit, and delete php xml nodes

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.