Add xml document content via php _ PHP Tutorial

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

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.