dom001.phpt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. --TEST--
  2. Test 1: Accessing single node
  3. --EXTENSIONS--
  4. dom
  5. --FILE--
  6. <?php
  7. require_once("dom_test.inc");
  8. echo "Test 1: accessing single nodes from php\n";
  9. $dom = new domDocument;
  10. $dom->loadxml($xmlstr);
  11. if(!$dom) {
  12. echo "Error while parsing the document\n";
  13. exit;
  14. }
  15. // children() of of document would result in a memleak
  16. //$children = $dom->children();
  17. //print_node_list($children);
  18. echo "--------- root\n";
  19. $rootnode = $dom->documentElement;
  20. print_node($rootnode);
  21. echo "--------- children of root\n";
  22. $children = $rootnode->childNodes;
  23. print_node_list($children);
  24. // The last node should be identical with the last entry in the children array
  25. echo "--------- last\n";
  26. $last = $rootnode->lastChild;
  27. print_node($last);
  28. // The parent of this last node is the root again
  29. echo "--------- parent\n";
  30. $parent = $last->parentNode;
  31. print_node($parent);
  32. // The children of this parent are the same children as one above
  33. echo "--------- children of parent\n";
  34. $children = $parent->childNodes;
  35. print_node_list($children);
  36. echo "--------- creating a new attribute\n";
  37. //This is worthless
  38. //$attr = $dom->createAttribute("src", "picture.gif");
  39. //print_r($attr);
  40. //$rootnode->set_attributeNode($attr);
  41. $attr = $rootnode->setAttribute("src", "picture.gif");
  42. $attr = $rootnode->getAttribute("src");
  43. print_r($attr);
  44. print "\n";
  45. echo "--------- Get Attribute Node\n";
  46. $attr = $rootnode->getAttributeNode("src");
  47. print_node($attr);
  48. echo "--------- Remove Attribute Node\n";
  49. $attr = $rootnode->removeAttribute("src");
  50. print "Removed " . $attr . " attributes.\n";
  51. echo "--------- attributes of rootnode\n";
  52. $attrs = $rootnode->attributes;
  53. print_node_list($attrs);
  54. echo "--------- children of an attribute\n";
  55. $children = $attrs->item(0)->childNodes;
  56. print_node_list($children);
  57. echo "--------- Add child to root\n";
  58. $myelement = new domElement("Silly", "Symphony");
  59. $newchild = $rootnode->appendChild($myelement);
  60. print_node($newchild);
  61. print $dom->saveXML();
  62. print "\n";
  63. echo "--------- Find element by tagname\n";
  64. echo " Using dom\n";
  65. $children = $dom->getElementsByTagname("Silly");
  66. print_node_list($children);
  67. echo " Using elem\n";
  68. $children = $rootnode->getElementsByTagName("Silly");
  69. print_node_list($children);
  70. echo "--------- Unlink Node\n";
  71. print_node($children->item(0));
  72. $rootnode->removeChild($children->item(0));
  73. print_node_list($rootnode->childNodes);
  74. print $dom->savexml();
  75. echo "--------- Find element by id\n";
  76. print ("Not implemented\n");
  77. echo "--------- Check various node_name return values\n";
  78. print ("Not needed\n");
  79. ?>
  80. --EXPECT--
  81. Test 1: accessing single nodes from php
  82. --------- root
  83. Node Name: chapter
  84. Node Type: 1
  85. Num Children: 4
  86. --------- children of root
  87. Node Name: title
  88. Node Type: 1
  89. Num Children: 1
  90. Node Content: Title
  91. Node Name: #text
  92. Node Type: 3
  93. Num Children: 0
  94. Node Content:
  95. Node Name: para
  96. Node Type: 1
  97. Num Children: 7
  98. Node Name: #text
  99. Node Type: 3
  100. Num Children: 0
  101. Node Content:
  102. --------- last
  103. Node Name: #text
  104. Node Type: 3
  105. Num Children: 0
  106. Node Content:
  107. --------- parent
  108. Node Name: chapter
  109. Node Type: 1
  110. Num Children: 4
  111. --------- children of parent
  112. Node Name: title
  113. Node Type: 1
  114. Num Children: 1
  115. Node Content: Title
  116. Node Name: #text
  117. Node Type: 3
  118. Num Children: 0
  119. Node Content:
  120. Node Name: para
  121. Node Type: 1
  122. Num Children: 7
  123. Node Name: #text
  124. Node Type: 3
  125. Num Children: 0
  126. Node Content:
  127. --------- creating a new attribute
  128. picture.gif
  129. --------- Get Attribute Node
  130. Node Name: src
  131. Node Type: 2
  132. Num Children: 1
  133. Node Content: picture.gif
  134. --------- Remove Attribute Node
  135. Removed 1 attributes.
  136. --------- attributes of rootnode
  137. Node Name: language
  138. Node Type: 2
  139. Num Children: 1
  140. Node Content: en
  141. --------- children of an attribute
  142. Node Name: #text
  143. Node Type: 3
  144. Num Children: 0
  145. Node Content: en
  146. --------- Add child to root
  147. Node Name: Silly
  148. Node Type: 1
  149. Num Children: 1
  150. Node Content: Symphony
  151. <?xml version="1.0" standalone="yes"?>
  152. <!DOCTYPE chapter SYSTEM "/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd" [
  153. <!ENTITY sp "spanish">
  154. ]>
  155. <!-- lsfj -->
  156. <chapter language="en"><title language="en">Title</title>
  157. <para language="ge">
  158. &sp;
  159. <!-- comment -->
  160. <informaltable language="&sp;kkk">
  161. <tgroup cols="3">
  162. <tbody>
  163. <row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
  164. <row><entry>a2</entry><entry>c2</entry></row>
  165. <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
  166. </tbody>
  167. </tgroup>
  168. </informaltable>
  169. </para>
  170. <Silly>Symphony</Silly></chapter>
  171. --------- Find element by tagname
  172. Using dom
  173. Node Name: Silly
  174. Node Type: 1
  175. Num Children: 1
  176. Node Content: Symphony
  177. Using elem
  178. Node Name: Silly
  179. Node Type: 1
  180. Num Children: 1
  181. Node Content: Symphony
  182. --------- Unlink Node
  183. Node Name: Silly
  184. Node Type: 1
  185. Num Children: 1
  186. Node Content: Symphony
  187. Node Name: title
  188. Node Type: 1
  189. Num Children: 1
  190. Node Content: Title
  191. Node Name: #text
  192. Node Type: 3
  193. Num Children: 0
  194. Node Content:
  195. Node Name: para
  196. Node Type: 1
  197. Num Children: 7
  198. Node Name: #text
  199. Node Type: 3
  200. Num Children: 0
  201. Node Content:
  202. <?xml version="1.0" standalone="yes"?>
  203. <!DOCTYPE chapter SYSTEM "/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd" [
  204. <!ENTITY sp "spanish">
  205. ]>
  206. <!-- lsfj -->
  207. <chapter language="en"><title language="en">Title</title>
  208. <para language="ge">
  209. &sp;
  210. <!-- comment -->
  211. <informaltable language="&sp;kkk">
  212. <tgroup cols="3">
  213. <tbody>
  214. <row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
  215. <row><entry>a2</entry><entry>c2</entry></row>
  216. <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
  217. </tbody>
  218. </tgroup>
  219. </informaltable>
  220. </para>
  221. </chapter>
  222. --------- Find element by id
  223. Not implemented
  224. --------- Check various node_name return values
  225. Not needed