PHP头条
热点:

如何正确使用PHP DOM-XML创建XML文件


我们在创建XML文件并对其进行解析时,通常都会用到PHP DOM-XML。那么如何才能正确的使用它来实现这一功能呢?下面我们就来仔细看下它的应用方法。

  • 深入探讨PHP命令行参数
  • 如何正确运用PHP XMLReader解析XML文档
  • 深入解读PHP DOMXPath在进行XML文件解析
  • 推荐几款功能强大的PHP模板引擎
  • PHP使用技巧的要点分析

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:urs@circle.ch, 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上运行

www.phpzy.comtrue/php/13593.htmlTechArticle如何正确使用PHP DOM-XML创建XML文件 我们在创建XML文件并对其进行解析时,通常都会用到PHP DOM-XML。那么如何才能正确的使用它来实现这一功能呢?下面我们就来仔细看下它的应用方法。...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐