recherche
Maisonphp教程php手册如何正确使用PHP DOM-XML创建XML文件

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

Jun 13, 2016 am 11:08 AM
phpxml使用创建exister如何nous文件heurecorrect解析conduirePasser

我们在创建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上运行


Déclaration
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques moisBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
1 Il y a quelques moisBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Vous avez un jeu croisé?
1 Il y a quelques moisBy尊渡假赌尊渡假赌尊渡假赌

Outils chauds

MinGW - GNU minimaliste pour Windows

MinGW - GNU minimaliste pour Windows

Ce projet est en cours de migration vers osdn.net/projects/mingw, vous pouvez continuer à nous suivre là-bas. MinGW : un port Windows natif de GNU Compiler Collection (GCC), des bibliothèques d'importation et des fichiers d'en-tête librement distribuables pour la création d'applications Windows natives ; inclut des extensions du runtime MSVC pour prendre en charge la fonctionnalité C99. Tous les logiciels MinGW peuvent fonctionner sur les plates-formes Windows 64 bits.

SublimeText3 version anglaise

SublimeText3 version anglaise

Recommandé : version Win, prend en charge les invites de code !

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Adaptateur de serveur SAP NetWeaver pour Eclipse

Adaptateur de serveur SAP NetWeaver pour Eclipse

Intégrez Eclipse au serveur d'applications SAP NetWeaver.

PhpStorm version Mac

PhpStorm version Mac

Le dernier (2018.2.1) outil de développement intégré PHP professionnel