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>

  1. $rootelement =$dom->createElement("article");
  2. $title =$dom->createElement("title", "PHP訪問MySql資料庫 初級篇");
複製代碼

然後建立帶常值內容的結點

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

也可以先產生結點再為其添加常值內容。

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

然後將和結點加入到結點中去

  1. $rootelement->appendChild($title);
  2. $rootelement->appendChild($link);
複製代碼

最後將結點加入到DOMDocument對象中,

  1. $dom->appendChild($rootelement);
複製代碼

這樣一個完整的XML就產生完畢了。再整出整個XML,

  1. echo $dom->saveXML() ;
複製代碼

saveXML()也可以只輸入部分XML文本,如echo $dom->saveXML($link);就只會輸出結點:http://blog.csdn.net/morewindows/article/details/7102362

下面再給出一個完整的PHP中資料內容輸出到XML檔案的例子。該例子會對將一個PHP數組輸出到XML檔案中。

  1. //將數組輸出到XML檔案中
  2. // by MoreWindows( http://blog.csdn.net/MoreWindows )
  3. $article_array = array(
  4. "第一篇" => array(
  5. "title"=>"PHP訪問MySql資料庫 初級篇",
  6. "link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
  7. ),
  8. "第二篇" => array(
  9. "title"=>"PHP訪問MySql資料庫 中級篇 Smarty技術",
  10. "link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
  11. ),
  12. "第三篇" => array(
  13. "title"=>"PHP訪問MySql資料庫 進階篇 AJAX技術",
  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檔案大小' . $dom->save($filename) . '位元組';
  32. ?>
複製代碼

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

  1. //將數組輸出到XML檔案中
  2. // by MoreWindows( http://blog.csdn.net/MoreWindows )
  3. $article_array = array(
  4. "第一篇" => array(
  5. "title"=>"PHP訪問MySql資料庫 初級篇",
  6. "link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
  7. ),
  8. "第二篇" => array(
  9. "title"=>"PHP訪問MySql資料庫 中級篇 Smarty技術",
  10. "link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
  11. ),
  12. "第三篇" => array(
  13. "title"=>"PHP訪問MySql資料庫 進階篇 AJAX技術",
  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檔案大小' . $dom->save($filename) . '位元組';
  32. ?>
複製代碼

運行該PHP會在D盤上產生test.xml檔案(Win7 + XAMPP + IE9.0測試通過)

二.讀取XML檔案以讀取前文中產生的D:test.xml為例:

  1. //讀取XML檔案

  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. //得到結點

  8. $articles = $dom->getElementsByTagName("article");
  9. echo ' 結點個數 ' . $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. //輸出結果

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

複製代碼

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

  1. //讀取XML檔案

  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. //得到結點

  8. $articles = $dom->getElementsByTagName("article");
  9. echo ' 結點個數 ' . $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. //輸出結果

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

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.