This article mainly introduces how to add xml document content through php, involving appendchild usage skills. if you need it, refer to the following example to illustrate how to add xml document content through php. Share it with you for your reference. The specific analysis is as follows:
The content of the xml document is described here. The Code continues from the previous "how to read xml content in DOM basics and php". the code is as follows:
The code is as follows:
<? Php
// 1. create a DOMDocument object. This object indicates an xml file.
$ Xmldoc = new DOMDocument ();
// 2. load the xml file (specify the xml file to be parsed, and the dom tree node will be loaded to the memory)
$ Xmldoc-> load ("class. xml ");
// 3. add a student information
// (1) retrieve the desired node
$ Root = $ xmldoc-> getElementsByTagName ("class")-> item (0); // return the DOMElement object type
Var_dump ($ root );
// (2) create student node student
$ Stu_node = $ xmldoc-> createElement ("student"); // returns the DOMElement object type.
$ Stu_node-> setAttribute ("id", "beauty"); // add attributes to the created node, if necessary
// (3) Create a node name, sex, and age.
$ Stu_node_name = $ xmldoc-> createElement ("name ");
$ Stu_node_name-> nodeValue = "Daqiao ";
$ Stu_node_sex = $ xmldoc-> createElement ("sex ");
$ Stu_node_sex-> nodeValue = "female ";
$ Stu_node_age = $ xmldoc-> createElement ("age ");
$ Stu_node_age-> nodeValue = "25 ";
// (4) mount the name, sex, age, and other three nodes to the student node
$ Stu_node-> appendchild ($ stu_node_name );
$ Stu_node-> appendchild ($ stu_node_sex );
$ Stu_node-> appendchild ($ stu_node_age );
// (5) mount the student node to the root node
$ Root-> appendchild ($ stu_node );
// 4. save it to the xml document
// $ Xmldoc-> save ("class. xml "); // save it to the original xml document, which is equivalent to adding it later. if it is an xml document that does not exist, a new xml document will be created, the content is the original xml content + new content.
?>
I hope this article will help you with the XML programming for php operations.