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