How to Add xml document content through php _ php skills

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

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.