Php xml file operation code (1)

Source: Internet
Author: User
If you want to create SimpleXML objects and learn how to operate xml, see. Example. xml file:

The code is as follows:


$ Xml = simplexml_load_file ('example. XML'); // Create a SimpleXML object
Print_r ($ xml); // output XML
?>


The code is as follows:





Production support


100001
Simon
24
1982-11-06
5000.00
1000.00


100002
Eline
24
1982-01-01
6000.00
2000.00




Testing center


110001
Helen
23
1983-07-21
5000.00
1000.00





The code is as follows:


$ Xml = simplexml_load_file ('example. XML'); // Create a SimpleXML object
Var_dump ($ xml); // output XML
?>


The code is as follows:


$ 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
"; // Output the name attribute
}
?>


The code is as follows:


$ Xml = simplexml_load_file ('example. XML'); // read the xml file
Echo $ xml-> depart-> name [0]; // output node
?>


The code is as follows:


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


The code is as follows:


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


The code is as follows:


$ Xml = simplexml_load_file ('example. XML'); // read xml
$ Xml-> depart-> name [0] = "Human Resource"; // modify a node
?>


The code is as follows:


$ Xml = simplexml_load_file ('example. XML'); // read xml data
Echo $ xml-> asXML (); // standardize XML data
?>


The code is as follows:


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

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.