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 "original 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--EXPECTF-- 60original has 5 nodes 61node 4 62Course: no title:DOMText 63~string(1) " 64" 65node 3 66Course: two:DOMElement 67~string(24) " 68 69 c2n1 70 c2n2 71 72 " 73node 2 74Course: no title:DOMText 75~string(2) " 76 " 77node 1 78Course: one:DOMElement 79~string(24) " 80 81 c1n1 82 c1n2 83 84 " 85node 0 86Course: no title:DOMText 87~string(2) " 88 " 89after text removed it now has 2 nodes 90node 0 91Course: one:DOMElement 92~string(24) " 93 94 c1n1 95 c1n2 96 97 " 98node 1 99Course: two:DOMElement 100~string(24) " 101 102 c2n1 103 c2n2 104 105 " 106