PHP XML file Operation implementation Code (II) _php Foundation

Source: Internet
Author: User
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);
}
}
}
?>

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.