DOMNode_cloneNode_basic.phpt 2.5 KB

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