PHP XML file operation code (i) _php Foundation

Source: Internet
Author: User
Example.xml file:
Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); Creating SimpleXML Objects
Print_r ($xml); Output XML
?>

Copy Code code as follows:

<?xml version= ' 1.0 '?>
<departs>
<depart>
<name>production support</name>
<employees>
<employee>
<serial_no>100001</serial_no>
<name>Simon</name>
<age>24</age>
<birthday>1982-11-06</birthday>
<salary>5000.00</salary>
<bonus>1000.00</bonus>
</employee>
<employee>
<serial_no>100002</serial_no>
<name>Elaine</name>
<age>24</age>
<birthday>1982-01-01</birthday>
<salary>6000.00</salary>
<bonus>2000.00</bonus>
</employee>
</employees>
</depart>
<depart>
<name>testing center</name>
<employees>
<employee>
<serial_no>110001</serial_no>
<name>Helen</name>
<age>23</age>
<birthday>1983-07-21</birthday>
<salary>5000.00</salary>
<bonus>1000.00</bonus>
</employee>
</employees>
</depart>
</departs>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); Creating SimpleXML Objects
Var_dump ($xml); Output XML
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); reading XML files
foreach ($xml->depart as $a)//loop read every depart tag in XML data
{
echo "$a->name <BR>"; Output the name attribute
}
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); reading XML files
echo $xml->depart->name[0]; Output node
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml ');
foreach ($xml->depart->children () as $depart)//loops read the child labels under the Depart label
{
Var_dump ($depart); XML data for Output label
}
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); reading XML files
$result = $xml->xpath ('/departs/depart/employees/employee/name '); Defining nodes
Var_dump ($result); Output node
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); Reading XML
$xml->depart->name[0] = "Human Resource"; modifying nodes
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); Reading XML data
echo $xml->asxml (); Standardize XML data
?>

Copy Code code as follows:

<?php
$xml = simplexml_load_file (' Example.xml '); Reading XML data
$newxml = $xml->asxml (); Standardize XML data
$fp = fopen ("Newxml.xml", "w"); Open the file you want to write XML data to
Fwrite ($fp, $newxml); Writing XML data
Fclose ($FP); Close File
?>

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.