自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何正確使用PHP DOM-XML創(chuàng)建XML文件

開發(fā) 后端
對(duì)于PHP語言的初學(xué)者來說,PHP DOM-XML還是比較陌生的,希望朋友們能通過這篇文章具體了解這方面的知識(shí),以加深對(duì)PHP的了解程度。

我們?cè)趧?chuàng)建XML文件并對(duì)其進(jìn)行解析時(shí),通常都會(huì)用到PHP DOM-XML。那么如何才能正確的使用它來實(shí)現(xiàn)這一功能呢?下面我們就來仔細(xì)看下它的應(yīng)用方法。#t#

PHP DOM-XML的應(yīng)用代碼示例:

  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:urs@circle.ch, 16-1-2001   
  7. *   
  8. */  
  9. // 使用PHP DOM-XML創(chuàng)建和解析XML文件  
  10. //創(chuàng)建XML文檔對(duì)象;以后的處理過程將在此基礎(chǔ)上進(jìn)行  
  11. doc = new_xmldoc("1.0" );  
  12. //創(chuàng)建根節(jié)點(diǎn),并設(shè)置一個(gè)屬性  
  13. root = $doc->add_root("faq" );  
  14. $root->setattr("page", "32" );  
  15. //子節(jié)點(diǎn)  
  16. one = $root->new_child("question", "");  
  17. //為子節(jié)點(diǎn)設(shè)置屬性  
  18. $one->setattr("number", "1");  
  19. //question也創(chuàng)建子節(jié)點(diǎn),并且給它賦值   
  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. // 創(chuàng)建一個(gè)不直接賦值的節(jié)點(diǎn)  
  29. twoone = $two->new_child("answer", "");  
  30. // 然后給它單獨(dú)賦值  
  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. //現(xiàn)在使用xpath從XML文檔中得到內(nèi)容  
  47. doc = xmldoc(join("", file("test_dom.xml")) );  
  48. ctx = xpath_new_context($doc );  
  49. //所有對(duì)象  
  50. foo = xpath_eval($ctx, "//child::*");  
  51. print_r($foo);  
  52. print("< br/>< br/>");  
  53. //text node 對(duì)象  
  54. foo = xpath_eval($ctx, "//text");  
  55. print_r($foo);  
  56. print("< br/>< br/>");  
  57. // ***個(gè)text node對(duì)象  
  58. foo = xpath_eval($ctx, "//text[1]");  
  59. print_r($foo);  
  60. print("< br/>< br/>");  
  61. // 第二個(gè)text node對(duì)象  
  62. foo = xpath_eval($ctx, "//text[2]");  
  63. print_r($foo);  
  64. print("< br/>< br/>");  
  65. // 第三個(gè)answer對(duì)象  
  66. foo = xpath_eval($ctx, "//answer[3]");  
  67. print_r($foo);  
  68. print("< br/>< br/>");  
  69. //第三個(gè)text node的類型,名稱和內(nèi)容   
  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上運(yùn)行

責(zé)任編輯:曹凱 來源: 王朝網(wǎng)絡(luò)
相關(guān)推薦

2013-06-08 13:29:27

Android開發(fā)DOM讀取XMLXML解析

2011-12-28 10:57:37

2009-04-23 13:19:21

創(chuàng)建XMLXML文件Javascript

2009-12-02 13:07:27

PHP XMLRead

2010-09-09 16:17:10

PHPXMLXML DOM

2010-09-28 11:11:23

XML DOMHTML DOM

2009-06-10 21:51:42

JavaScript XMLFirefox

2009-11-02 11:02:58

VB.NET XML文

2013-06-08 13:02:11

Android開發(fā)XML解析

2022-03-22 09:41:31

Java編程語言持久化

2009-01-18 11:45:16

PHPXML網(wǎng)站編程

2010-09-28 10:03:15

DOM文檔對(duì)象模型

2010-09-09 13:55:47

XML DOM

2010-09-09 13:40:19

XML DOM

2009-01-19 10:03:58

C#XML動(dòng)態(tài)分層菜單

2009-01-04 16:58:48

LINQ to XMLLINQXML

2010-09-09 17:19:07

HTML DOMXML DOM

2010-09-28 10:24:50

HTML DOMXML DOM

2011-06-15 09:02:01

Qt QDomDocume XML

2010-09-09 13:12:29

XML DOM
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)