xref: /PHP-7.1/ext/dom/tests/dom001.phpt (revision 7af945e2)
1--TEST--
2Test 1: Accessing single node
3--SKIPIF--
4<?php require_once('skipif.inc'); ?>
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
118
119Node Name: para
120Node Type: 1
121Num Children: 7
122
123Node Name: #text
124Node Type: 3
125Num Children: 0
126Node Content:
127
128
129--------- last
130Node Name: #text
131Node Type: 3
132Num Children: 0
133Node Content:
134
135
136--------- parent
137Node Name: chapter
138Node Type: 1
139Num Children: 4
140
141--------- children of parent
142Node Name: title
143Node Type: 1
144Num Children: 1
145Node Content: Title
146
147Node Name: #text
148Node Type: 3
149Num Children: 0
150Node Content:
151
152
153Node Name: para
154Node Type: 1
155Num Children: 7
156
157Node Name: #text
158Node Type: 3
159Num Children: 0
160Node Content:
161
162
163--------- creating a new attribute
164picture.gif
165--------- Get Attribute Node
166Node Name: src
167Node Type: 2
168Num Children: 1
169Node Content: picture.gif
170
171--------- Remove Attribute Node
172Removed 1 attributes.
173--------- attributes of rootnode
174Node Name: language
175Node Type: 2
176Num Children: 1
177Node Content: en
178
179--------- children of an attribute
180Node Name: #text
181Node Type: 3
182Num Children: 0
183Node Content: en
184
185--------- Add child to root
186Node Name: Silly
187Node Type: 1
188Num Children: 1
189Node Content: Symphony
190
191<?xml version="1.0" standalone="yes"?>
192<!DOCTYPE chapter SYSTEM "/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd" [
193<!ENTITY sp "spanish">
194]>
195<!-- lsfj  -->
196<chapter language="en"><title language="en">Title</title>
197<para language="ge">
198&sp;
199<!-- comment -->
200<informaltable language="&sp;kkk">
201<tgroup cols="3">
202<tbody>
203<row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
204<row><entry>a2</entry><entry>c2</entry></row>
205<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
206</tbody>
207</tgroup>
208</informaltable>
209</para>
210<Silly>Symphony</Silly></chapter>
211
212--------- Find element by tagname
213    Using dom
214Node Name: Silly
215Node Type: 1
216Num Children: 1
217Node Content: Symphony
218
219    Using elem
220Node Name: Silly
221Node Type: 1
222Num Children: 1
223Node Content: Symphony
224
225--------- Unlink Node
226Node Name: Silly
227Node Type: 1
228Num Children: 1
229Node Content: Symphony
230
231Node Name: title
232Node Type: 1
233Num Children: 1
234Node Content: Title
235
236Node Name: #text
237Node Type: 3
238Num Children: 0
239Node Content:
240
241
242Node Name: para
243Node Type: 1
244Num Children: 7
245
246Node Name: #text
247Node Type: 3
248Num Children: 0
249Node Content:
250
251
252<?xml version="1.0" standalone="yes"?>
253<!DOCTYPE chapter SYSTEM "/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd" [
254<!ENTITY sp "spanish">
255]>
256<!-- lsfj  -->
257<chapter language="en"><title language="en">Title</title>
258<para language="ge">
259&sp;
260<!-- comment -->
261<informaltable language="&sp;kkk">
262<tgroup cols="3">
263<tbody>
264<row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
265<row><entry>a2</entry><entry>c2</entry></row>
266<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
267</tbody>
268</tgroup>
269</informaltable>
270</para>
271</chapter>
272--------- Find element by id
273Not implemented
274--------- Check various node_name return values
275Not needed
276