DOMNode_removeChild_basic.phpt 2.0 KB

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