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