DOMNode_removeChild_basic.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. --TEST--
  2. DOM removeChild : Basic Functionality
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. ?>
  7. --CREDITS--
  8. Simon Hughes <odbc3@hotmail.com>
  9. --FILE--
  10. <?php
  11. $xml = <<< EOXML
  12. <?xml version="1.0" encoding="ISO-8859-1"?>
  13. <courses>
  14. <course title="one">
  15. <notes>
  16. <note>c1n1</note>
  17. <note>c1n2</note>
  18. </notes>
  19. </course>
  20. <course title="two">
  21. <notes>
  22. <note>c2n1</note>
  23. <note>c2n2</note>
  24. </notes>
  25. </course>
  26. </courses>
  27. EOXML;
  28. function dumpcourse($current) {
  29. $title = ($current->nodeType != XML_TEXT_NODE && $current->hasAttribute('title')) ? $current->getAttribute('title'):"no title";
  30. echo "Course: $title:";echo get_class($current), "\n";
  31. echo "~";var_dump($current->textContent);
  32. }
  33. $dom = new DOMDocument();
  34. $dom->loadXML($xml);
  35. $root = $dom->documentElement;
  36. $children = $root->childNodes;
  37. $len = $children->length;
  38. echo "original has $len nodes\n";
  39. for ($index = $children->length - 1; $index >=0; $index--) {
  40. echo "node $index\n";
  41. $current = $children->item($index);
  42. dumpcourse($current);
  43. if ($current->nodeType == XML_TEXT_NODE) {
  44. $noderemoved = $root->removeChild($current);
  45. }
  46. }
  47. $children = $root->childNodes;
  48. $len = $children->length;
  49. echo "after text removed it now has $len nodes\n";
  50. for ($index = 0; $index < $children->length; $index++) {
  51. echo "node $index\n";
  52. $current = $children->item($index);
  53. dumpcourse($current);
  54. }
  55. --EXPECTF--
  56. original has 5 nodes
  57. node 4
  58. Course: no title:DOMText
  59. ~string(1) "
  60. "
  61. node 3
  62. Course: two:DOMElement
  63. ~string(24) "
  64. c2n1
  65. c2n2
  66. "
  67. node 2
  68. Course: no title:DOMText
  69. ~string(2) "
  70. "
  71. node 1
  72. Course: one:DOMElement
  73. ~string(24) "
  74. c1n1
  75. c1n2
  76. "
  77. node 0
  78. Course: no title:DOMText
  79. ~string(2) "
  80. "
  81. after text removed it now has 2 nodes
  82. node 0
  83. Course: one:DOMElement
  84. ~string(24) "
  85. c1n1
  86. c1n2
  87. "
  88. node 1
  89. Course: two:DOMElement
  90. ~string(24) "
  91. c2n1
  92. c2n2
  93. "