php讀寫xml檔案的方法介紹

來源:互聯網
上載者:User
  1. header("content-type:text/html; charset=utf-8"); //指定PHP使用UTF-8編碼
  2. $xml = simplexml_load_file("example.xml"); //讀取xml檔案
  3. $newxml = $xml->asXML(); //標準化$xml
  4. $fp = fopen("newxml.xml", "w"); //建立xml檔案
  5. fwrite($fp, $newxml); //寫入-------xml檔案
  6. fclose($fp);
複製代碼

php可以方便的產生和讀取xml檔案。主要通過DOMDocument、DOMElement和DOMNodeList來完成XML的讀取與寫入操作。

下面為大家介紹如何使用這些類,供大家學習參考。

一.產生XML檔案對於一個如下XML檔案。

  1. PHP訪問mysql資料庫 初級篇
  2. http://blog.csdn.net/morewindows/article/details/7102362
  3. PHP訪問MySql資料庫 初級篇
  4. http://blog.csdn.net/morewindows/article/details/7102362
複製代碼

我們來看看如何用PHP來產生:

首先new一個DOMDocument對象並設定編碼格式。

  1. $dom = newDOMDocument('1.0', 'UTF-8');
  2. $dom->formatOutput= true;
複製代碼

再建立結點和結點 </p><ol><li><li>$rootelement =$dom->createElement("article");<li>$title =$dom->createElement("title", "PHP訪問MySql資料庫 初級篇");</ol><em onclick="copycode($('code_qiv'));">複製代碼</em><p>然後建立帶常值內容的<link>結點 </p><ol><li><li>$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");</ol><em onclick="copycode($('code_rRR'));">複製代碼</em><p>也可以先產生<link>結點再為其添加常值內容。 </p><ol><li><li>$link = $dom->createElement("link");<li>$linktext =$dom->createTextNode('http://blog.csdn.net/morewindows/article/details/7102362');<li>$link->appendChild($linktext);</ol><em onclick="copycode($('code_wcK'));">複製代碼</em><p>然後將<title>和<link>結點加入到結點中去 </p><ol><li><li>$rootelement->appendChild($title);<li>$rootelement->appendChild($link);</ol><em onclick="copycode($('code_zKk'));">複製代碼</em><p>最後將結點加入到DOMDocument對象中, </p><ol><li><li>$dom->appendChild($rootelement);</ol><em onclick="copycode($('code_fY4'));">複製代碼</em><p>這樣一個完整的XML就產生完畢了。再整出整個XML, </p><ol><li><li>echo $dom->saveXML() ;</ol><em onclick="copycode($('code_O6A'));">複製代碼</em><p>saveXML()也可以只輸入部分XML文本,如echo $dom->saveXML($link);就只會輸出<link>結點:<link>http://blog.csdn.net/morewindows/article/details/7102362</link></p><p>下面再給出一個完整的PHP中資料內容輸出到XML檔案的例子。該例子會對將一個PHP數組輸出到XML檔案中。 </p><ol><li><li><?php<li>//將數組輸出到XML檔案中<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$article_array = array(<li>"第一篇" => array(<li>"title"=>"PHP訪問MySql資料庫 初級篇",<li>"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"<li>),<li>"第二篇" => array(<li>"title"=>"PHP訪問MySql資料庫 中級篇 Smarty技術",<li>"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"<li>),<li>"第三篇" => array(<li>"title"=>"PHP訪問MySql資料庫 進階篇 AJAX技術",<li>"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"<li>),<li>);<li>$dom = new DOMDocument('1.0', 'UTF-8');<li>$dom->formatOutput = true;<li>$rootelement = $dom->createElement("MoreWindows");<li>foreach ($article_array as $key=>$value)<li>{<li>$article = $dom->createElement("article", $key);<li>$title = $dom->createElement("title", $value['title']);<li>$link = $dom->createElement("link", $value['link']);<li>$article->appendChild($title);<li>$article->appendChild($link);<li>$rootelement->appendChild($article);<li>}<li>$dom->appendChild($rootelement);<li>$filename = "D:test.xml";<li>echo 'XML檔案大小' . $dom->save($filename) . '位元組';<li>?></ol><em onclick="copycode($('code_du9'));">複製代碼</em><p>#------------------- </p><ol><li><li><?php<li>//將數組輸出到XML檔案中<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$article_array = array(<li>"第一篇" => array(<li>"title"=>"PHP訪問MySql資料庫 初級篇",<li>"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"<li>),<li>"第二篇" => array(<li>"title"=>"PHP訪問MySql資料庫 中級篇 Smarty技術",<li>"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"<li>),<li>"第三篇" => array(<li>"title"=>"PHP訪問MySql資料庫 進階篇 AJAX技術",<li>"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"<li>),<li>);<li>$dom = new DOMDocument('1.0', 'UTF-8');<li>$dom->formatOutput = true;<li>$rootelement = $dom->createElement("MoreWindows");<li>foreach ($article_array as $key=>$value)<li>{<li>$article = $dom->createElement("article", $key);<li>$title = $dom->createElement("title", $value['title']);<li>$link = $dom->createElement("link", $value['link']);<li>$article->appendChild($title);<li>$article->appendChild($link);<li>$rootelement->appendChild($article);<li>}<li>$dom->appendChild($rootelement);<li>$filename = "D:test.xml";<li>echo 'XML檔案大小' . $dom->save($filename) . '位元組';<li>?></ol><em onclick="copycode($('code_LNR'));">複製代碼</em><p>運行該PHP會在D盤上產生test.xml檔案(Win7 + XAMPP + IE9.0測試通過)</p><p><u><strong>二.讀取XML檔案</strong></u>以讀取前文中產生的D:test.xml為例: </p><ol><li><li><li><p><?php<li>//讀取XML檔案<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$filename = "D:test.xml";<li>$article_array = array();</p><li><p>$dom = new DOMDocument('1.0', 'UTF-8');<li>$dom->load($filename);</p><li><p>//得到結點<li>$articles = $dom->getElementsByTagName("article");<li>echo ' 結點個數 ' . $articles->length;<li>foreach ($articles as $article)<li>{<li>$id = $article->getElementsByTagName("id")->item(0)->nodeValue;<li>$title = $article->getElementsByTagName("title")->item(0)->nodeValue;<li>$link = $article->getElementsByTagName("link")->item(0)->nodeValue;<li>$article_array[$id] = array('title'=>$title, 'link'=>$link);<li>}</p><li><p>//輸出結果<li>echo "<pre class="brush:php;toolbar:false">";<li>var_dump($article_array);<li>echo "</pre>";<li>?></p><li></ol><em onclick="copycode($('code_oek'));">複製代碼</em><p>#----------------- </p><ol><li><li><li><p><?php<li>//讀取XML檔案<li>// by MoreWindows( http://blog.csdn.net/MoreWindows )<li>$filename = "D:test.xml";<li>$article_array = array();</p><li><p>$dom = new DOMDocument('1.0', 'UTF-8');<li>$dom->load($filename);</p><li><p>//得到結點<li>$articles = $dom->getElementsByTagName("article");<li>echo ' 結點個數 ' . $articles->length;<li>foreach ($articles as $article)<li>{<li>$id = $article->getElementsByTagName("id")->item(0)->nodeValue;<li>$title = $article->getElementsByTagName("title")->item(0)->nodeValue;<li>$link = $article->getElementsByTagName("link")->item(0)->nodeValue;<li>$article_array[$id] = array('title'=>$title, 'link'=>$link);<li>}</p><li><p>//輸出結果<li>echo "<pre class="brush:php;toolbar:false">";<li>var_dump($article_array);<li>echo "</pre>";<li>?></p><li></ol><em onclick="copycode($('code_PhZ'));">複製代碼</em></td></tr></table><li ><i class="layui-icon">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.