DOMNode_replaceChild_basic.phpt 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. --TEST--
  2. Replacing a child node
  3. --EXTENSIONS--
  4. dom
  5. --CREDITS--
  6. Matt Raines <matt@raines.me.uk>
  7. #London TestFest 2008
  8. --FILE--
  9. <?php
  10. $document = new DOMDocument();
  11. $document->loadXML('<?xml version="1.0" encoding="utf-8"?>
  12. <root><foo><bar/><baz/></foo><spam><eggs/><eggs/></spam></root>');
  13. // Replaces the child node oldChild with newChild in the list of children, and
  14. // returns the oldChild node.
  15. $parent = $document->getElementsByTagName('foo')->item(0);
  16. $new_child = $document->createElement('qux');
  17. $old_child = $parent->replaceChild($new_child, $parent->firstChild);
  18. echo "New child replaces old child:\n" . $document->saveXML();
  19. echo "Old child is returned:\n" . $old_child->tagName . "\n";
  20. // If the newChild is already in the tree, it is first removed.
  21. $parent = $document->getElementsByTagName('spam')->item(0);
  22. $parent->replaceChild($new_child, $parent->firstChild);
  23. echo "Existing child is removed from tree:\n" . $document->saveXML();
  24. // Children are inserted in the correct order.
  25. $new_child = $document->getElementsByTagName('spam')->item(0);
  26. $parent = $document->getElementsByTagName('foo')->item(0);
  27. $parent->replaceChild($new_child, $parent->firstChild);
  28. echo "Children are inserted in order:\n" . $document->saveXML();
  29. ?>
  30. --EXPECT--
  31. New child replaces old child:
  32. <?xml version="1.0" encoding="utf-8"?>
  33. <root><foo><qux/><baz/></foo><spam><eggs/><eggs/></spam></root>
  34. Old child is returned:
  35. bar
  36. Existing child is removed from tree:
  37. <?xml version="1.0" encoding="utf-8"?>
  38. <root><foo><baz/></foo><spam><qux/><eggs/></spam></root>
  39. Children are inserted in order:
  40. <?xml version="1.0" encoding="utf-8"?>
  41. <root><foo><spam><qux/><eggs/></spam></foo></root>