DOMNode_cloneNode_basic.phpt 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. --TEST--
  2. DOM cloneNode : 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. // strip all text nodes from this tree
  37. $children = $root->childNodes;
  38. $len = $children->length;
  39. for ($index = $children->length - 1; $index >=0; $index--) {
  40. $current = $children->item($index);
  41. if ($current->nodeType == XML_TEXT_NODE) {
  42. $noderemoved = $root->removeChild($current);
  43. }
  44. }
  45. echo "Start cloneNode test\n";
  46. $first_course = $children->item(0);
  47. $cloned_first_course_default = $first_course->cloneNode();
  48. $first_course->setAttribute('title', 'new title1');
  49. $cloned_first_course_true = $first_course->cloneNode(true);
  50. $first_course->setAttribute('title', 'new title2');
  51. $cloned_first_course_false = $first_course->cloneNode(false);
  52. $first_course->setAttribute('title', 'new title3');
  53. $cloned_first_course_default->setAttribute('title', 'new title default');
  54. $cloned_first_course_true->setAttribute('title', 'new title true');
  55. $cloned_first_course_false->setAttribute('title', 'new title false');
  56. $root->appendChild($cloned_first_course_default);
  57. $root->appendChild($cloned_first_course_true);
  58. $root->appendChild($cloned_first_course_false);
  59. $children = $root->childNodes;
  60. for ($index = 0; $index < $children->length; $index++) {
  61. echo "node $index\n";
  62. dumpcourse($children->item($index));
  63. }
  64. --EXPECTF--
  65. Start cloneNode test
  66. node 0
  67. Course: new title3:DOMElement
  68. ~string(24) "
  69. c1n1
  70. c1n2
  71. "
  72. node 1
  73. Course: two:DOMElement
  74. ~string(24) "
  75. c2n1
  76. c2n2
  77. "
  78. node 2
  79. Course: new title default:DOMElement
  80. ~string(0) ""
  81. node 3
  82. Course: new title true:DOMElement
  83. ~string(24) "
  84. c1n1
  85. c1n2
  86. "
  87. node 4
  88. Course: new title false:DOMElement
  89. ~string(0) ""