- Header ("content-type: text/html; charset = utf-8"); // specifies that PHP uses UTF-8 encoding
- $ Xml = simplexml_load_file ("example. xml"); // read the xml file
- $ Newxml = $ xml-> asXML (); // standardize $ xml
- $ Fp = fopen ("newxml. xml", "w"); // Create an xml file
- Fwrite ($ fp, $ newxml); // write the ------- xml file
- 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.
-
- PHP accessing mysql database
- Http://blog.csdn.net/morewindows/article/details/7102362 </link>
-
- PHP accessing MySql database
- 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.
- $ Dom = newDOMDocument ('1. 0', 'utf-8 ');
- $ Dom-> formatOutput = true;
Create a node andNode </p>
- $ Rootelement = $ dom-> createElement ("article ");
- $ Title = $ dom-> createElement ("title", "PHP accessing MySql database ");
Then create Node
- $ Link = $ dom-> createElement ("link", "http://blog.csdn.net/morewindows/article/details/7102362 ");
Or Mr. Cheng Add text content to the node.
- $ Link = $ dom-> createElement ("link ");
- $ Linktext = $ dom-> createTextNode ('http: // blog.csdn.net/morewindows/article/details/7102362 ');
- $ Link-> appendChild ($ linktext );
ThenAnd Add a node to the node
- $ Rootelement-> appendChild ($ title );
- $ Rootelement-> appendChild ($ link );
Add the node to the DOMDocument object,
- $ Dom-> appendChild ($ rootelement );
The complete XML file is generated. And then the entire XML,
- 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.
- // Output the array to the XML file
- // By MoreWindows (http://blog.csdn.net/MoreWindows)
- $ Article_array = array (
- "Article 1" => array (
- "Title" => "accessing MySql database using PHP ",
- "Link" => "http://blog.csdn.net/morewindows/article/details/7102362"
- ),
- "Article 2" => array (
- "Title" => "accessing MySql database using PHP intermediate Smarty technology ",
- "Link" => "http://blog.csdn.net/morewindows/article/details/7094642"
- ),
- "Article 3" => array (
- "Title" => "PHP advanced AJAX technology for MySql database access ",
- "Link" => "http://blog.csdn.net/morewindows/article/details/7086524"
- ),
- );
- $ Dom = new DOMDocument ('1. 0', 'utf-8 ');
- $ Dom-> formatOutput = true;
- $ Rootelement = $ dom-> createElement ("MoreWindows ");
- Foreach ($ article_array as $ key => $ value)
- {
- $ Article = $ dom-> createElement ("article", $ key );
- $ Title = $ dom-> createElement ("title", $ value ['title']);
- $ Link = $ dom-> createElement ("link", $ value ['link']);
- $ Article-> appendChild ($ title );
- $ Article-> appendChild ($ link );
- $ Rootelement-> appendChild ($ article );
- }
- $ Dom-> appendChild ($ rootelement );
- $ Filename = "D: test. xml ";
- Echo 'XML file size'. $ dom-> save ($ filename). 'Byte ';
- ?>
#-------------------
- // Output the array to the XML file
- // By MoreWindows (http://blog.csdn.net/MoreWindows)
- $ Article_array = array (
- "Article 1" => array (
- "Title" => "accessing MySql database using PHP ",
- "Link" => "http://blog.csdn.net/morewindows/article/details/7102362"
- ),
- "Article 2" => array (
- "Title" => "accessing MySql database using PHP intermediate Smarty technology ",
- "Link" => "http://blog.csdn.net/morewindows/article/details/7094642"
- ),
- "Article 3" => array (
- "Title" => "PHP advanced AJAX technology for MySql database access ",
- "Link" => "http://blog.csdn.net/morewindows/article/details/7086524"
- ),
- );
- $ Dom = new DOMDocument ('1. 0', 'utf-8 ');
- $ Dom-> formatOutput = true;
- $ Rootelement = $ dom-> createElement ("MoreWindows ");
- Foreach ($ article_array as $ key => $ value)
- {
- $ Article = $ dom-> createElement ("article", $ key );
- $ Title = $ dom-> createElement ("title", $ value ['title']);
- $ Link = $ dom-> createElement ("link", $ value ['link']);
- $ Article-> appendChild ($ title );
- $ Article-> appendChild ($ link );
- $ Rootelement-> appendChild ($ article );
- }
- $ Dom-> appendChild ($ rootelement );
- $ Filename = "D: test. xml ";
- Echo 'XML file size'. $ dom-> save ($ filename). 'Byte ';
- ?>
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:
// Read the XML file
- // By MoreWindows (http://blog.csdn.net/MoreWindows)
- $ Filename = "D: test. xml ";
- $ Article_array = array ();
$ Dom = new DOMDocument ('1. 0', 'utf-8 ');
- $ Dom-> load ($ filename );
// Obtain the node
- $ Articles = $ dom-> getElementsByTagName ("article ");
- Echo 'number of knot'. $ articles-> length;
- Foreach ($ articles as $ article)
- {
- $ Id = $ article-> getElementsByTagName ("id")-> item (0)-> nodeValue;
- $ Title = $ article-> getElementsByTagName ("title")-> item (0)-> nodeValue;
- $ Link = $ article-> getElementsByTagName ("link")-> item (0)-> nodeValue;
- $ Article_array [$ id] = array ('title' => $ title, 'link' => $ link );
- }
// Output result
- Echo"
"; - var_dump($article_array);
- echo "
";
- ?>
#-----------------
// Read the XML file
- // By MoreWindows (http://blog.csdn.net/MoreWindows)
- $ Filename = "D: test. xml ";
- $ Article_array = array ();
$ Dom = new DOMDocument ('1. 0', 'utf-8 ');
- $ Dom-> load ($ filename );
// Obtain the node
- $ Articles = $ dom-> getElementsByTagName ("article ");
- Echo 'number of knot'. $ articles-> length;
- Foreach ($ articles as $ article)
- {
- $ Id = $ article-> getElementsByTagName ("id")-> item (0)-> nodeValue;
- $ Title = $ article-> getElementsByTagName ("title")-> item (0)-> nodeValue;
- $ Link = $ article-> getElementsByTagName ("link")-> item (0)-> nodeValue;
- $ Article_array [$ id] = array ('title' => $ title, 'link' => $ link );
- }
// Output result
- Echo"
"; - var_dump($article_array);
- echo "
";
- ?>
|