Example. xml file:
Copy codeThe Code is as follows:
<? Php
$ Xml = simplexml_load_file ('example. xml'); // create a SimpleXML object
Print_r ($ xml); // output XML
?>
Copy codeThe 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
?>