Example. xml file:
CopyCode The Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // create a simplexml object
Print_r ($ XML); // output XML
?>
Copy code The Code is 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> elasticity </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 codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // create a simplexml object
Var_dump ($ XML); // output XML
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // read the XML file
Foreach ($ XML-> depart as $ A) // read each depart tag in XML data cyclically
{
Echo "$ A-> name <br>"; // output the name attribute.
}
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // read the XML file
Echo $ XML-> depart-> name [0]; // output Node
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml ');
Foreach ($ XML-> depart-> Children () as $ depart) // read sub-tags under the depart tag cyclically
{
Var_dump ($ depart); // output the XML data of the tag
}
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // read the XML file
$ Result = $ XML-> XPath ('/departs/depart/employees/employee/name'); // define a node
Var_dump ($ result); // output Node
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // read XML
$ XML-> depart-> name [0] = "human resource"; // modify a node
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // read XML data
Echo $ XML-> asxml (); // standardize XML data
?>
Copy codeThe Code is as follows: <? PHP
$ Xml = simplexml_load_file ('example. xml'); // read XML data
$ Newxml = $ XML-> asxml (); // standardize XML data
$ Fp = fopen ("newxml. xml", "W"); // open the file to write XML data
Fwrite ($ FP, $ newxml); // write XML data
Fclose ($ FP); // close the file
?>