程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 如何正確使用PHP DOM-XML創建XML文件

如何正確使用PHP DOM-XML創建XML文件

編輯:關於PHP編程

我們在創建XML文件並對其進行解析時,通常都會用到

PHP DOM-XML的應用代碼示例:

  1. < ?php  
  2. /**   
  3. * Topic: Create and parse XML files using PHP DOM-XML   
  4. * Source:http://www.php.net/domxml   
  5. * Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html   
  6. * Author:[email protected], 16-1-2001   
  7. *   
  8. */  
  9. // 使用PHP DOM-XML創建和解析XML文件  
  10. //創建XML文檔對象;以後的處理過程將在此基礎上進行  
  11. doc = new_xmldoc("1.0" );  
  12. //創建根節點,並設置一個屬性  
  13. root = $doc->add_root("faq" );  
  14. $root->setattr("page", "32" );  
  15. //子節點  
  16. one = $root->new_child("question", "");  
  17. //為子節點設置屬性  
  18. $one->setattr("number", "1");  
  19. //question也創建子節點,並且給它賦值   
  20. $one->new_child("text", "1. Where to get libxml-2.0.0?");  
  21. $one->new_child("answer", "You can download the latest   
  22. release of libxml either as a source archive or   
  23. RPM package from http://www.xmlsoft.org.   
  24. The current version is libxml2-2.2.1." );  
  25. two = $root->new_child("question", "" );  
  26. $two->setattr("number", "2");  
  27. $two->new_child("text", "2. How to configure PHP4?" );  
  28. // 創建一個不直接賦值的節點  
  29. twoone = $two->new_child("answer", "");  
  30. // 然後給它單獨賦值  
  31. $twoone->set_content("DIR is the libxml install directory   
  32. (if you just use --with-dom it defaults   
  33. to /usr), I needed to use --with-dom=/usr/local" );  
  34. three = $root->new_child("question", "" );  
  35. $three->setattr("number", "7" );  
  36. $three->new_child("text", "7. How to use DOM XML function ?" );  
  37. $three->new_child("answer", "Read this document source for   
  38. a simple example." );  
  39. //輸出到Browser   
  40. print("< pre>".htmlspecialchars($doc->dumpmem() )."< /pre>" );  
  41. // write to file  
  42. //寫回到文件   
  43. fp = fopen("test_dom.xml", "w+" );  
  44. fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() ));  
  45. fclose($fp);  
  46. //現在使用xpath從XML文檔中得到內容  
  47. doc = xmldoc(join("", file("test_dom.xml")) );  
  48. ctx = xpath_new_context($doc );  
  49. //所有對象  
  50. foo = xpath_eval($ctx, "//child::*");  
  51. print_r($foo);  
  52. print("< br/>< br/>");  
  53. //text node 對象  
  54. foo = xpath_eval($ctx, "//text");  
  55. print_r($foo);  
  56. print("< br/>< br/>");  
  57. // 第一個text node對象  
  58. foo = xpath_eval($ctx, "//text[1]");  
  59. print_r($foo);  
  60. print("< br/>< br/>");  
  61. // 第二個text node對象  
  62. foo = xpath_eval($ctx, "//text[2]");  
  63. print_r($foo);  
  64. print("< br/>< br/>");  
  65. // 第三個answer對象  
  66. foo = xpath_eval($ctx, "//answer[3]");  
  67. print_r($foo);  
  68. print("< br/>< br/>");  
  69. //第三個text node的類型,名稱和內容   
  70. foo = xpath_eval($ctx, "//text[3]");  
  71. tmp = $foo->nodeset;  
  72. print_r($tmp);  
  73. print("< br/>");  
  74. print($tmp[0]->type) . "; ";  
  75. print($tmp[0]->name) . "; ";  
  76. print($tmp[0]->content);  
  77. ?> 

需要說明,PHP DOM-XML只能在PHPPHP4.0.x + linux上運行


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved