Add xml document content using php. The code for adding xml document content through php is as follows :? Php1: Create a DOMDocument object. This object indicates the xml file $ xmldocnewDOMDocument (); 2. load the xml file (specify the method for adding xml document content through php)
The code is as follows:
// 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.
?>
The pipeline code is as follows :? Php // 1. create a DOMDocument object. This object indicates the xml file $ xmldoc = new DOMDocument (); // 2. load the xml file (specify...