Some simple examples of php operating xml files

WBOY
Release: 2016-07-25 08:59:07
Original
810 people have browsed it
  1. /**

  2. * php operation xml file
  3. * edit bbs.it-home.org
  4. */
  5. //Example 1
  6. $xml = simplexml_load_file('example.xml'); //Create SimpleXML object
  7. var_dump($ xml); //Output XML

  8. //Example 2

  9. $xml = simplexml_load_file('example.xml'); //Read XML file
  10. foreach($xml->depart as $ a) //Loop through each part tag in the XML data
  11. {
  12. echo "$a->name
    "; //Output the name attribute
  13. }

  14. $xml = simplexml_load_file('example.xml'); //Read XML file
  15. echo $xml->depart->name[0]; //Output node

  16. //Example 4

  17. $xml = simplexml_load_file('example.xml');
  18. foreach ($xml->depart->children() as $depart) //Loop to read the children under the depart tag Tag
  19. {
  20. var_dump($depart); //Output the XML data of the tag
  21. }

  22. //Example 5

  23. $xml = simplexml_load_file('example.xml'); //Read XML file
  24. $result = $xml->xpath('/departs/depart/employees/employee/name'); //Define node
  25. var_dump($result); //Output node

  26. < p>//Example 6
  27. $xml = simplexml_load_file('example.xml'); //Read XML
  28. $xml->depart->name[0] = "Human Resource"; //Modify node< /p>
  29. //Example 7

  30. $xml = simplexml_load_file('example.xml'); //Read XML data
  31. echo $xml->asXML(); //Standardized XML data
  32. //Example 8

  33. $xml = simplexml_load_file('example.xml'); //Read XML data
  34. $newxml = $xml->asXML(); //Standardize XML data
  35. $ fp = fopen("newxml.xml", "w"); //Open the file to write XML data
  36. fwrite($fp, $newxml); //Write XML data
  37. fclose($fp); // Close the file
  38. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!