PHP adds XML documents to XML document content by PHP
The example in this article describes how to add XML document content through PHP. Share to everyone for your reference. The specific analysis is as follows:
Here is the add XML document content, from the previous "Dom Foundation and PHP read XML content Operation Method" continues, the code is as follows:
Copy CodeThe code is as follows: <?php
1. Create a DOMDocument object. The object represents the XML file
$xmldoc = new DOMDocument ();
2. Load the XML file (specifies which XML file to parse, at which point the DOM tree node is loaded into memory)
$xmldoc->load ("Class.xml");
3. Add a Student information
(1) Remove the node
$root = $xmldoc->getelementsbytagname ("Class")->item (0);//Return DomElement object type
Var_dump ($root);
(2) Create student node student
$stu _node = $xmldoc->createelement ("student");//Return DomElement object type
$stu _node->setattribute ("id", "BBW");//Add attributes to the created node, if necessary
(3) Create names, genders, ages, etc. node name, sex, and age
$stu _node_name = $xmldoc->createelement ("name");
$stu _node_name->nodevalue = "Big Joe";
$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 on the student node
$stu _node->appendchild ($stu _node_name);
$stu _node->appendchild ($stu _node_sex);
$stu _node->appendchild ($stu _node_age);
(5) Attach the student node to the root node
$root->appendchild ($stu _node);
4. Save to XML document
$xmldoc->save ("Class.xml");//saved to the original XML document, equivalent to adding later; if it is a nonexistent XML document, a new XML document will be created with the contents of the original XML content + the newly added content.
?>
I hope this article is helpful to everyone's PHP operation XML programming.
http://www.bkjia.com/PHPjc/946737.html www.bkjia.com true http://www.bkjia.com/PHPjc/946737.html techarticle By adding XML document content in PHP, PHP adds an XML document that describes how to add XML document content through PHP. Share to everyone for your reference. The specific analysis is as follows: this ...