xref: /PHP-8.2/ext/dom/tests/dom001.phpt (revision bd9f4fa6)
1--TEST--
2Test 1: Accessing single node
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7require_once("dom_test.inc");
8
9echo "Test 1: accessing single nodes from php\n";
10$dom = new domDocument;
11$dom->loadxml($xmlstr);
12if(!$dom) {
13  echo "Error while parsing the document\n";
14  exit;
15}
16
17// children() of of document would result in a memleak
18//$children = $dom->children();
19//print_node_list($children);
20
21echo "--------- root\n";
22$rootnode = $dom->documentElement;
23print_node($rootnode);
24
25echo "--------- children of root\n";
26$children = $rootnode->childNodes;
27print_node_list($children);
28
29// The last node should be identical with the last entry in the children array
30echo "--------- last\n";
31$last = $rootnode->lastChild;
32print_node($last);
33
34// The parent of this last node is the root again
35echo "--------- parent\n";
36$parent = $last->parentNode;
37print_node($parent);
38
39// The children of this parent are the same children as one above
40echo "--------- children of parent\n";
41$children = $parent->childNodes;
42print_node_list($children);
43
44echo "--------- creating a new attribute\n";
45//This is worthless
46//$attr = $dom->createAttribute("src", "picture.gif");
47//print_r($attr);
48
49//$rootnode->set_attributeNode($attr);
50$attr = $rootnode->setAttribute("src", "picture.gif");
51$attr = $rootnode->getAttribute("src");
52print_r($attr);
53print "\n";
54
55echo "--------- Get Attribute Node\n";
56$attr = $rootnode->getAttributeNode("src");
57print_node($attr);
58
59echo "--------- Remove Attribute Node\n";
60$attr = $rootnode->removeAttribute("src");
61print "Removed " . $attr . " attributes.\n";
62
63echo "--------- attributes of rootnode\n";
64$attrs = $rootnode->attributes;
65print_node_list($attrs);
66
67echo "--------- children of an attribute\n";
68$children = $attrs->item(0)->childNodes;
69print_node_list($children);
70
71echo "--------- Add child to root\n";
72$myelement = new domElement("Silly", "Symphony");
73$newchild = $rootnode->appendChild($myelement);
74print_node($newchild);
75print $dom->saveXML();
76print "\n";
77
78echo "--------- Find element by tagname\n";
79echo "    Using dom\n";
80$children = $dom->getElementsByTagname("Silly");
81print_node_list($children);
82
83echo "    Using elem\n";
84$children = $rootnode->getElementsByTagName("Silly");
85print_node_list($children);
86
87echo "--------- Unlink Node\n";
88print_node($children->item(0));
89$rootnode->removeChild($children->item(0));
90print_node_list($rootnode->childNodes);
91print $dom->savexml();
92
93echo "--------- Find element by id\n";
94print ("Not implemented\n");
95
96echo "--------- Check various node_name return values\n";
97print ("Not needed\n");
98
99?>
100--EXPECT--
101Test 1: accessing single nodes from php
102--------- root
103Node Name: chapter
104Node Type: 1
105Num Children: 4
106
107--------- children of root
108Node Name: title
109Node Type: 1
110Num Children: 1
111Node Content: Title
112
113Node Name: #text
114Node Type: 3
115Num Children: 0
116Node Content:
117
118Node Name: para
119Node Type: 1
120Num Children: 7
121
122Node Name: #text
123Node Type: 3
124Num Children: 0
125Node Content:
126
127--------- last
128Node Name: #text
129Node Type: 3
130Num Children: 0
131Node Content:
132
133--------- parent
134Node Name: chapter
135Node Type: 1
136Num Children: 4
137
138--------- children of parent
139Node Name: title
140Node Type: 1
141Num Children: 1
142Node Content: Title
143
144Node Name: #text
145Node Type: 3
146Num Children: 0
147Node Content:
148
149Node Name: para
150Node Type: 1
151Num Children: 7
152
153Node Name: #text
154Node Type: 3
155Num Children: 0
156Node Content:
157
158--------- creating a new attribute
159picture.gif
160--------- Get Attribute Node
161Node Name: src
162Node Type: 2
163Num Children: 1
164Node Content: picture.gif
165
166--------- Remove Attribute Node
167Removed 1 attributes.
168--------- attributes of rootnode
169Node Name: language
170Node Type: 2
171Num Children: 1
172Node Content: en
173
174--------- children of an attribute
175Node Name: #text
176Node Type: 3
177Num Children: 0
178Node Content: en
179
180--------- Add child to root
181Node Name: Silly
182Node Type: 1
183Num Children: 1
184Node Content: Symphony
185
186<?xml version="1.0" standalone="yes"?>
187<!DOCTYPE chapter SYSTEM "/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd" [
188<!ENTITY sp "spanish">
189]>
190<!-- lsfj  -->
191<chapter language="en"><title language="en">Title</title>
192<para language="ge">
193&sp;
194<!-- comment -->
195<informaltable language="&sp;kkk">
196<tgroup cols="3">
197<tbody>
198<row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
199<row><entry>a2</entry><entry>c2</entry></row>
200<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
201</tbody>
202</tgroup>
203</informaltable>
204</para>
205<Silly>Symphony</Silly></chapter>
206
207--------- Find element by tagname
208    Using dom
209Node Name: Silly
210Node Type: 1
211Num Children: 1
212Node Content: Symphony
213
214    Using elem
215Node Name: Silly
216Node Type: 1
217Num Children: 1
218Node Content: Symphony
219
220--------- Unlink Node
221Node Name: Silly
222Node Type: 1
223Num Children: 1
224Node Content: Symphony
225
226Node Name: title
227Node Type: 1
228Num Children: 1
229Node Content: Title
230
231Node Name: #text
232Node Type: 3
233Num Children: 0
234Node Content:
235
236Node Name: para
237Node Type: 1
238Num Children: 7
239
240Node Name: #text
241Node Type: 3
242Num Children: 0
243Node Content:
244
245<?xml version="1.0" standalone="yes"?>
246<!DOCTYPE chapter SYSTEM "/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd" [
247<!ENTITY sp "spanish">
248]>
249<!-- lsfj  -->
250<chapter language="en"><title language="en">Title</title>
251<para language="ge">
252&sp;
253<!-- comment -->
254<informaltable language="&sp;kkk">
255<tgroup cols="3">
256<tbody>
257<row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
258<row><entry>a2</entry><entry>c2</entry></row>
259<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
260</tbody>
261</tgroup>
262</informaltable>
263</para>
264</chapter>
265--------- Find element by id
266Not implemented
267--------- Check various node_name return values
268Not needed
269