温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-10-18 16:53:15 来源:亿速云 阅读:125 作者:柒染 栏目:编程语言

如何正确使用PHP DOM-XML创建XML文件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

我们在创建XML文件并对其进行解析时,通常都会用到PHP DOM-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: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上运行

关于如何正确使用PHP DOM-XML创建XML文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI