Home  >  Article  >  Backend Development  >  Introduction to the method of reading and writing xml files in php

Introduction to the method of reading and writing xml files in php

WBOY
WBOYOriginal
2016-07-25 09:03:38851browse
  1. header("content-type:text/html; charset=utf-8"); //Specify PHP to use UTF-8 encoding
  2. $xml = simplexml_load_file("example.xml"); //Read xml file
  3. $newxml = $xml->asXML(); //Standardize $xml
  4. $fp = fopen("newxml.xml", "w"); //New xml file
  5. fwrite($fp, $newxml ); //Write -------xml file
  6. fclose($fp);
Copy code

php can easily generate and read xml files. XML reading and writing operations are mainly completed through DOMDocument, DOMElement and DOMNodeList.

The following is an introduction to how to use these classes for your reference.

One. Generate XML file For an XML file as follows.

  1. PHP access to mysql database primary article
  2. < ;article>
  3. Preliminary article on accessing MySql database with PHP
  4. http://blog.csdn.net/morewindows/article/details/7102362
Copy code

Let’s see how to generate it with PHP:

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

  1. $dom = newDOMDocument('1.0', 'UTF-8');
  2. $dom->formatOutput= true;
Copy the code

Create nodes and

nodes <div class="blockcode"> <div id="code_qiv"><ol> <li> <li>$rootelement =$dom->createElement("article");</li> <li>$title =$dom->createElement("title", "PHP Access MySql Database Elementary Article");</li> </ol></div> <em onclick="copycode($('code_qiv'));">Copy Code </em> </div> <p> and then create a <link> node with text content </p> <div class="blockcode"> <div id="code_rRR"><ol> <li> <li>$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");</li> </ol></div> <em onclick="copycode($('code_rRR'));">Copy code</em> </div> <p>also You can generate the <link> node first and then add text content to it. </p> <div class="blockcode"> <div id="code_wcK"><ol> <li> <li>$link = $dom->createElement("link");</li> <li>$linktext =$dom->createTextNode('http://blog.csdn.net/morewindows/article/details/7102362 ');</li> <li>$link->appendChild($linktext);</li> </ol></div> <em onclick="copycode($('code_wcK'));">Copy the code</em> </div> <p> and then add the </p> <title> and <link> nodes to the nodes <div class="blockcode"> <div id="code_zKk"><ol> <li> <li>$rootelement->appendChild($title);</li> <li>$rootelement->appendChild($link);</li> </ol></div> <em onclick="copycode($('code_zKk'));">Copy the code</em> </div> <p>Finally add the node to the DOMDocument object, </p> <div class="blockcode"> <div id="code_fY4"><ol> <li> <li>$dom->appendChild($rootelement);</li> </ol></div> <em onclick="copycode($('code_fY4'));">Copy the code</em> </div> <p>A complete XML is generated. Then regenerate the entire XML, </p> <div class="blockcode"> <div id="code_O6A"><ol> <li> <li>echo $dom->saveXML() ;</li> </ol></div> <em onclick="copycode($('code_O6A'));">Copy code</em> </div> <p>saveXML() can also input only part of the XML text, such as echo $dom->saveXML($link); it will only output the <link>node: <link>http://blog.csdn.net/morewindows/article/details/7102362 </p> <p>The following is a complete example of outputting data content to an XML file in PHP. This example will output a PHP array to an XML file. </p> <div class="blockcode"> <div id="code_du9"><ol> <li> <li><?php<li>//Output the array into an XML file<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$article_array = array(<li>"First Article" => array(</li> <li>"title"=>"Initial PHP Access to MySql Database",</li> <li>"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"</li> <li>), </li> <li>"Part 2" => array(</li> <li>"title"=>"Intermediate article Smarty technology for PHP access to MySql database",</li> <li>"link"=>"http://blog.csdn.net/morewindows/article /details/7094642"</li> <li>),</li> <li>"Part 3" => array(</li> <li>"title"=>"Advanced AJAX Technology for PHP to Access MySql Database",</li> <li>"link"=>"http://blog .csdn.net/morewindows/article/details/7086524"</li> <li>),</li> <li>);</li> <li>$dom = new DOMDocument('1.0', 'UTF-8');</li> <li>$dom->formatOutput = true;</li> <li>$rootelement = $dom->createElement("MoreWindows");</li> <li>foreach ($article_array as $key=>$value)</li> <li>{</li> <li>$article = $dom->createElement("article", $key);</li> <li>$ title = $dom->createElement("title", $value['title']);</li> <li>$link = $dom->createElement("link", $value['link']);</li> <li>$article- >appendChild($title);</li> <li>$article->appendChild($link);</li> <li>$rootelement->appendChild($article);</li> <li>}</li> <li>$dom->appendChild($rootelement);</li> <li>$filename = "D:test.xml";</li> <li>echo 'XML file size' . $dom->save($filename) . 'bytes';</li> <li>?></li> </ol></div> <em onclick="copycode($('code_du9'));">Copy code</em> </div> <p>#----- --------------- </p> <div class="blockcode"> <div id="code_LNR"><ol> <li> <li><?php<li>//Output the array into an XML file<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$article_array = array(<li>"First Article" => array(</li> <li>"title"=>"Initial PHP Access to MySql Database",</li> <li>"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"</li> <li>), </li> <li>"Part 2" => array(</li> <li>"title"=>"Intermediate article Smarty technology for PHP access to MySql database",</li> <li>"link"=>"http://blog.csdn.net/morewindows/article /details/7094642"</li> <li>),</li> <li>"Part 3" => array(</li> <li>"title"=>"Advanced AJAX Technology for PHP to Access MySql Database",</li> <li>"link"=>"http://blog .csdn.net/morewindows/article/details/7086524"</li> <li>),</li> <li>);</li> <li>$dom = new DOMDocument('1.0', 'UTF-8');</li> <li>$dom->formatOutput = true;</li> <li>$rootelement = $dom->createElement("MoreWindows");</li> <li>foreach ($article_array as $key=>$value)</li> <li>{</li> <li>$article = $dom->createElement("article", $key);</li> <li>$ title = $dom->createElement("title", $value['title']);</li> <li>$link = $dom->createElement("link", $value['link']);</li> <li>$article- >appendChild($title);</li> <li>$article->appendChild($link);</li> <li>$rootelement->appendChild($article);</li> <li>}</li> <li>$dom->appendChild($rootelement);</li> <li>$filename = "D:test.xml";</li> <li>echo 'XML file size' . $dom->save($filename) . 'bytes';</li> <li>?></li> </ol></div> <em onclick="copycode($('code_LNR'));">Copy code</em> </div> <p> Running this PHP will generate the test.xml file on the D drive (Win7 + XAMPP + IE9.0 test passed)</p> <p><u><strong>II. Read XML files </strong></u> Take reading the D:test.xml generated in the previous article as an example: </p> <div class="blockcode"> <div id="code_oek"><ol> <li> <li> <li><p><?php<li>//Read XML file<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$filename = "D:test.xml ";<li>$article_array = array();</p></li> <li><p>$dom = new DOMDocument('1.0', 'UTF-8');</li> <li>$dom->load($filename);< ;/p></li> <li><p>//Get<article>node</li> <li>$articles = $dom->getElementsByTagName("article");</li> <li>echo '<article>Number of nodes' . $articles- >length;</li> <li>foreach ($articles as $article)</li> <li>{</li> <li>$id = $article->getElementsByTagName("id")->item(0)->nodeValue;</li> <li>$title = $article-> ;getElementsByTagName("title")->item(0)->nodeValue;</li> <li>$link = $article->getElementsByTagName("link")->item(0)->nodeValue;</li> <li>$article_array[ $id] = array('title'=>$title, 'link'=>$link);</li> <li>}</p></li> <li><p>//Output result</li> <li>echo "<pre class="brush:php;toolbar:false">"; </li> <li>var_dump($article_array);</li> <li>echo "</pre>";</li> <li>?></p></li> <li> </ol></div> <em onclick="copycode($('code_oek'));">Copy code</em> </div> <p>#-------------------------- --- </p> <div class="blockcode"> <div id="code_PhZ"><ol> <li> <li> <li><p><?php<li>//Read XML file<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$filename = "D:test.xml ";<li>$article_array = array();</p></li> <li><p>$dom = new DOMDocument('1.0', 'UTF-8');</li> <li>$dom->load($filename);< ;/p></li> <li><p>//Get<article>node</li> <li>$articles = $dom->getElementsByTagName("article");</li> <li>echo '<article>Number of nodes' . $articles- >length;</li> <li>foreach ($articles as $article)</li> <li>{</li> <li>$id = $article->getElementsByTagName("id")->item(0)->nodeValue;</li> <li>$title = $article-> ;getElementsByTagName("title")->item(0)->nodeValue;</li> <li>$link = $article->getElementsByTagName("link")->item(0)->nodeValue;</li> <li>$article_array[ $id] = array('title'=>$title, 'link'=>$link);</li> <li>}</p></li> <li><p>//Output result</li> <li>echo "<pre class="brush:php;toolbar:false">"; </li> <li>var_dump($article_array);</li> <li>echo "</pre>";</li> <li>?></p></li> <li> </ol></div> <em onclick="copycode($('code_PhZ'));">Copy code</em> </div>


Statement:
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