Copy code code as follows:
<?php
Create a new DOM document
$dom = new DOMDocument ();
Create a departs label on the root node
$departs = $dom->createelement (' departs ');
$dom->appendchild ($departs);
Create a depart child label under the Departs tab
$depart = $dom->createelement (' Depart ');
$departs->appendchild ($depart);
Create a employees child label under the Depart tab
$employees = $dom->createelement (' employees ');
$depart->appendchild ($employees);
Create an employee child label under the Employees tab
$employee = $dom->createelement (' employee ');
$employees->appendchild ($employee);
Create a SERIAL_NO child label under the Employee tab
$serial _no = $dom->createelement (' serial_no ');
$employee->appendchild ($serial _no);
Add a value node for the Serial_no tab 100001
$serial _no_value = $dom->createtextnode (' 100001 ');
$serial _no->appendchild ($serial _no_value);
Output XML data
echo $dom->savexml ();
?>
Copy Code code as follows:
<?php
$dom = new DOMDocument (); Creating a DOM Object
$dom->load (' example.xml '); reading XML files
$root = $dom->documentelement; Get root of XML 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 read each child node
{
if ($e->nodetype = = Xml_text_node)//If the child node is a text type output
{
echo $e->nodevalue. " <BR> ";
}
else if ($e->nodetype = = Xml_element_node)//If the child node is a node object, call function processing
{
Read_child ($e);
}
}
}
?>