Php xml file operation implementation code (2) _ PHP Tutorial

Source: Internet
Author: User
Php xml file operation implementation code (2 ). Copy the code as follows :? Create a new DOM document in php $ domnewDomDocument (); create the departs tag in the root node $ departs $ dom-createElement (departs); $ dom-appendChild The code is as follows:


// Create a new DOM document
$ Dom = new DomDocument ();
// Create a departs tag on the root node
$ Departs = $ dom-> createElement ('demopart ');
$ Dom-> appendChild ($ departs );
// Create a depart subtag under The departs tag
$ Depart = $ dom-> createElement ('demo ');
$ Departs-> appendChild ($ depart );
// Create an employees sub-tag under the depart tag
$ Employees = $ dom-> createElement ('ployees ');
$ Depart-> appendChild ($ employees );
// Create the employee subtag under the employees tag
$ Employee = $ dom-> createElement ('employee ');
$ Employees-> appendChild ($ employee );
// Create the serial_no sub-tag under the employee tag
$ Serial_no = $ dom-> createElement ('serial _ no ');
$ Employee-> appendChild ($ serial_no );
// Add value node 100001 for the serial_no label
$ Serial_no_value = $ dom-> createTextNode ('20140901 ');
$ Serial_no-> appendChild ($ serial_no_value );
// Output XML data
Echo $ dom-> saveXML ();
?>



The code is as follows:


$ Dom = new DomDocument (); // Create a DOM object
$ Dom-> load ('example. XML'); // read the xml file
$ Root = $ dom-> documentElement; // get the root of XML data
Read_child ($ root); // call the read_child function to read the root object

Function read_child ($ node)
{
$ Children = $ node-> childNodes; // Obtain all child nodes of $ node

Foreach ($ children as $ e) // read each subnode cyclically
{
If ($ e-> nodeType = XML_TEXT_NODE) // if the child node is text, the output
{
Echo $ e-> nodeValue ."
";
}
Else if ($ e-> nodeType = XML_ELEMENT_NODE) // if the child node is a node object, call the function for processing.
{
Read_child ($ e );
}
}
}
?>

The http://www.bkjia.com/PHPjc/320038.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320038.htmlTechArticle code is as follows :? Php // create a new DOM document $ dom = new DomDocument (); // Create The departs tag $ departs = $ dom-createElement ('demopart') on the root node '); $ dom-appendChild...

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.