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