|
|
1.1 misho 1: --TEST--
2: DOM removeChild : Basic Functionality
3: --SKIPIF--
4: <?php
5: require_once('skipif.inc');
6: ?>
7: --CREDITS--
8: Simon 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>
28: EOXML;
29:
30: function dumpcourse($current) {
31: $title = ($current->nodeType != XML_TEXT_NODE && $current->hasAttribute('title')) ? $current->getAttribute('title'):"no title";
32: echo "Course: $title:";var_dump($current);
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;
42: echo "orignal has $len nodes\n";
43: for ($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;
53: echo "after text removed it now has $len nodes\n";
54: for ($index = 0; $index < $children->length; $index++) {
55: echo "node $index\n";
56: $current = $children->item($index);
57: dumpcourse($current);
58: }
59:
60: --EXPECTF--
61: orignal has 5 nodes
62: node 4
63: Course: no title:object(DOMText)#4 (0) {
64: }
65: ~string(1) "
66: "
67: node 3
68: Course: two:object(DOMElement)#5 (0) {
69: }
70: ~string(24) "
71:
72: c2n1
73: c2n2
74:
75: "
76: node 2
77: Course: no title:object(DOMText)#6 (0) {
78: }
79: ~string(2) "
80: "
81: node 1
82: Course: one:object(DOMElement)#4 (0) {
83: }
84: ~string(24) "
85:
86: c1n1
87: c1n2
88:
89: "
90: node 0
91: Course: no title:object(DOMText)#5 (0) {
92: }
93: ~string(2) "
94: "
95: after text removed it now has 2 nodes
96: node 0
97: Course: one:object(DOMElement)#3 (0) {
98: }
99: ~string(24) "
100:
101: c1n1
102: c1n2
103:
104: "
105: node 1
106: Course: two:object(DOMElement)#4 (0) {
107: }
108: ~string(24) "
109:
110: c2n1
111: c2n2
112:
113: "