How php reads and writes xml files

Source: Internet
Author: User
How php reads and writes xml files

  1. Header ("content-type: text/html; charset = utf-8"); // specifies that PHP uses UTF-8 encoding
  2. $ Xml = simplexml_load_file ("example. xml"); // read the xml file
  3. $ Newxml = $ xml-> asXML (); // standardize $ xml
  4. $ Fp = fopen ("newxml. xml", "w"); // Create an xml file
  5. Fwrite ($ fp, $ newxml); // write the ------- xml file
  6. Fclose ($ fp );

Php can easily generate and read xml files. Mainly through DOMDocument, DOMElement and DOMNodeList to read and write XML.

The following describes how to use these classes for your reference.

1. generate an XML fileFor the following XML file.

  1. PHP accessing mysql database
  2. Http://blog.csdn.net/morewindows/article/details/7102362 </link>
  3. PHP accessing MySql database
  4. Http://blog.csdn.net/morewindows/article/details/7102362 </link>

Let's take a look at how to use PHP to generate:

First, create a new DOMDocument object and set the encoding format.

  1. $ Dom = newDOMDocument ('1. 0', 'utf-8 ');
  2. $ Dom-> formatOutput = true;

Create a node andNode </p>

  1. $ Rootelement = $ dom-> createElement ("article ");
  2. $ Title = $ dom-> createElement ("title", "PHP accessing MySql database ");

Then create Node

  1. $ Link = $ dom-> createElement ("link", "http://blog.csdn.net/morewindows/article/details/7102362 ");

Or Mr. Cheng Add text content to the node.

  1. $ Link = $ dom-> createElement ("link ");
  2. $ Linktext = $ dom-> createTextNode ('http: // blog.csdn.net/morewindows/article/details/7102362 ');
  3. $ Link-> appendChild ($ linktext );

ThenAnd Add a node to the node

  1. $ Rootelement-> appendChild ($ title );
  2. $ Rootelement-> appendChild ($ link );

Add the node to the DOMDocument object,

  1. $ Dom-> appendChild ($ rootelement );

The complete XML file is generated. And then the entire XML,

  1. Echo $ dom-> saveXML ();

SaveXML () can also input only some XML text, such as echo $ dom-> saveXML ($ link); only Node: Http://blog.csdn.net/morewindows/article/details/7102362

The following example shows how to output the complete PHP Data content to an XML file. In this example, a PHP array is output to an XML file.

  1. // Output the array to the XML file
  2. // By MoreWindows (http://blog.csdn.net/MoreWindows)
  3. $ Article_array = array (
  4. "Article 1" => array (
  5. "Title" => "accessing MySql database using PHP ",
  6. "Link" => "http://blog.csdn.net/morewindows/article/details/7102362"
  7. ),
  8. "Article 2" => array (
  9. "Title" => "accessing MySql database using PHP intermediate Smarty technology ",
  10. "Link" => "http://blog.csdn.net/morewindows/article/details/7094642"
  11. ),
  12. "Article 3" => array (
  13. "Title" => "PHP advanced AJAX technology for MySql database access ",
  14. "Link" => "http://blog.csdn.net/morewindows/article/details/7086524"
  15. ),
  16. );
  17. $ Dom = new DOMDocument ('1. 0', 'utf-8 ');
  18. $ Dom-> formatOutput = true;
  19. $ Rootelement = $ dom-> createElement ("MoreWindows ");
  20. Foreach ($ article_array as $ key => $ value)
  21. {
  22. $ Article = $ dom-> createElement ("article", $ key );
  23. $ Title = $ dom-> createElement ("title", $ value ['title']);
  24. $ Link = $ dom-> createElement ("link", $ value ['link']);
  25. $ Article-> appendChild ($ title );
  26. $ Article-> appendChild ($ link );
  27. $ Rootelement-> appendChild ($ article );
  28. }
  29. $ Dom-> appendChild ($ rootelement );
  30. $ Filename = "D: test. xml ";
  31. Echo 'XML file size'. $ dom-> save ($ filename). 'Byte ';
  32. ?>

#-------------------

  1. // Output the array to the XML file
  2. // By MoreWindows (http://blog.csdn.net/MoreWindows)
  3. $ Article_array = array (
  4. "Article 1" => array (
  5. "Title" => "accessing MySql database using PHP ",
  6. "Link" => "http://blog.csdn.net/morewindows/article/details/7102362"
  7. ),
  8. "Article 2" => array (
  9. "Title" => "accessing MySql database using PHP intermediate Smarty technology ",
  10. "Link" => "http://blog.csdn.net/morewindows/article/details/7094642"
  11. ),
  12. "Article 3" => array (
  13. "Title" => "PHP advanced AJAX technology for MySql database access ",
  14. "Link" => "http://blog.csdn.net/morewindows/article/details/7086524"
  15. ),
  16. );
  17. $ Dom = new DOMDocument ('1. 0', 'utf-8 ');
  18. $ Dom-> formatOutput = true;
  19. $ Rootelement = $ dom-> createElement ("MoreWindows ");
  20. Foreach ($ article_array as $ key => $ value)
  21. {
  22. $ Article = $ dom-> createElement ("article", $ key );
  23. $ Title = $ dom-> createElement ("title", $ value ['title']);
  24. $ Link = $ dom-> createElement ("link", $ value ['link']);
  25. $ Article-> appendChild ($ title );
  26. $ Article-> appendChild ($ link );
  27. $ Rootelement-> appendChild ($ article );
  28. }
  29. $ Dom-> appendChild ($ rootelement );
  30. $ Filename = "D: test. xml ";
  31. Echo 'XML file size'. $ dom-> save ($ filename). 'Byte ';
  32. ?>

Run PHP to generate the test. xml file on drive D (Win7 + XAMPP + IE9.0 passed the test)

II. read XML filesTake the generated D: test. xml as an example:

  1. // Read the XML file

  2. // By MoreWindows (http://blog.csdn.net/MoreWindows)
  3. $ Filename = "D: test. xml ";
  4. $ Article_array = array ();

  5. $ Dom = new DOMDocument ('1. 0', 'utf-8 ');

  6. $ Dom-> load ($ filename );

  7. // Obtain the node

  8. $ Articles = $ dom-> getElementsByTagName ("article ");
  9. Echo 'number of knot'. $ articles-> length;
  10. Foreach ($ articles as $ article)
  11. {
  12. $ Id = $ article-> getElementsByTagName ("id")-> item (0)-> nodeValue;
  13. $ Title = $ article-> getElementsByTagName ("title")-> item (0)-> nodeValue;
  14. $ Link = $ article-> getElementsByTagName ("link")-> item (0)-> nodeValue;
  15. $ Article_array [$ id] = array ('title' => $ title, 'link' => $ link );
  16. }

  17. // Output result

  18. Echo"
    ";
  19. var_dump($article_array);
  20. echo "
  21. ";
  22. ?>

#-----------------

  1. // Read the XML file

  2. // By MoreWindows (http://blog.csdn.net/MoreWindows)
  3. $ Filename = "D: test. xml ";
  4. $ Article_array = array ();

  5. $ Dom = new DOMDocument ('1. 0', 'utf-8 ');

  6. $ Dom-> load ($ filename );

  7. // Obtain the node

  8. $ Articles = $ dom-> getElementsByTagName ("article ");
  9. Echo 'number of knot'. $ articles-> length;
  10. Foreach ($ articles as $ article)
  11. {
  12. $ Id = $ article-> getElementsByTagName ("id")-> item (0)-> nodeValue;
  13. $ Title = $ article-> getElementsByTagName ("title")-> item (0)-> nodeValue;
  14. $ Link = $ article-> getElementsByTagName ("link")-> item (0)-> nodeValue;
  15. $ Article_array [$ id] = array ('title' => $ title, 'link' => $ link );
  16. }

  17. // Output result

  18. Echo"
    ";
  19. var_dump($article_array);
  20. echo "
  21. ";
  22. ?>

Related Article

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.