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...