Home > Backend Development > PHP Tutorial > PHP xml file operation implementation code (2)_PHP tutorial

PHP xml file operation implementation code (2)_PHP tutorial

WBOY
Release: 2016-07-21 15:46:51
Original
924 people have browsed it

Copy the code The code is as follows:

//Create a new DOM document
$dom = new DomDocument();
//Create the departments tag at the root node
$departs = $dom->createElement('departs');
$dom->appendChild($departs);
//Create the department sub-tag under the departments tag
$depart = $dom->createElement('depart');
$departs->appendChild($depart);
//In department Create employees sub-tag under the tag
$employees = $dom->createElement('employees');
$depart->appendChild($employees);
//Create employees sub-tag under the employees tag Tag
$employee = $dom->createElement('employee');
$employees->appendChild($employee);
//Create serial_no sub-tag under employee tag
$ serial_no = $dom->createElement('serial_no');
$employee->appendChild($serial_no);
//Add value node 100001 for the serial_no tag
$serial_no_value = $dom-> ;createTextNode('100001');
$serial_no->appendChild($serial_no_value);
//Output XML data
echo $dom->saveXML();
?>


Copy code The code is as follows:

$dom = new DomDocument(); //Create DOM object
$dom->load('example.xml'); //Read XML file
$root = $dom->documentElement; //Get XML The root of the data
read_child($root); //Call the read_child function to read the root object

function read_child($node)
{
$children = $node->childNodes; //Get all child nodes of $node

foreach($children as $e) //Loop to read each child node
{
if($e->nodeType == XML_TEXT_NODE ) //If the child node is text type, output
{
echo $e->nodeValue."
";
}
else if($e->nodeType = = XML_ELEMENT_NODE) ​​//If the child node is a node object, call the function processing
{
read_child($e);
}
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320038.htmlTechArticleCopy the code as follows: ?php //Create a new DOM document $dom = new DomDocument(); / /Create the departments tag at the root node $departs = $dom-createElement('departs'); $dom-appendChild...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template