asXML(); //标准化$xml$fp = fopen("newxml.xml">
输入banner图图片脚本导航/分类

php读写xml文件的方法介绍

  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>然后创建带文本内容的结点 </p><ol><li><li>$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");</ol><em onclick="copycode($('code_rRR'));"></em><p>也可以先生成结点再为其添加文本内容。 </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>和结点加入到结点中去 </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);就只会</pre>输出结点:http://blog.csdn.net/morewindows/article/details/7102362</link></p> <p>下面再给出一个完整的PHP中数据内容</pre>输出到XML文件的例子。该例子会对将一个PHP数组</pre>输出到XML文件中。 </p><ol><li><li><?php<li>//将数组</pre>输出到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>//将数组</pre>输出到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>//</pre>输出结果<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>//</pre>输出结果<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> </div> </div> <script type="text/javascript" src="/layui/layui.js"></script> <script> layui.use('code', function(){ layui.code({ elem: 'pre', //默认值为.layui-code about:false, title:'php读写xml文件的方法介绍代码块', encode: true //是否转义html标签。默认不开启 }); }); </script> <footer class="comm-footer"> <ul class="channels"> <li> <a target="_top" href="/system/">操作系统</a> </li><li> <a target="_top" href="/down/">程序下载</a> </li><li> <a target="_top" href="/jquery/">jquery插件库</a> </li><li> <a target="_top" href="/jiaoben/">编程</a> </li><li> <a target="_top" href="/db/">数据库</a> </li><li> <a target="_top" href="/tools/">在线工具</a> </li><li> <a target="_top" href="/framebook/">框架书籍</a> </li><li> <a target="_top" href="/xitongjiaocheng/">系统教程</a> </li><li> <a target="_top" href="/ruanjianxiazai/">软件下载</a> </li> </ul> <ul class="versions"> <li> <a href="https://m.gxlcms.com" class="active">触屏版</a> </li> <li> <a href="https://www.gxlcms.com" target="_top">电脑版</a> </li> </ul> <p><a href="https://beian.miit.gov.cn/" target="_blank" style="color:#ffffff"></a><script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6c9b8cea1903c9f813f150c04aeebc3c"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script></p> </footer> </body> </html>