xref: /PHP-7.2/ext/dom/examples/dom1.php (revision 902d39a3)
1<?php
2require_once("dom1.inc");
3
4echo "Test 1: accessing single nodes from php\n";
5$dom = new domDocument;
6$dom->loadxml($xmlstr);
7if(!$dom) {
8  echo "Error while parsing the document\n";
9  exit;
10}
11
12// children() of of document would result in a memleak
13//$children = $dom->children();
14//print_node_list($children);
15
16echo "--------- root\n";
17$rootnode = $dom->documentElement;
18print_node($rootnode);
19
20echo "--------- children of root\n";
21$children = $rootnode->childNodes;
22print_node_list($children);
23
24// The last node should be identical with the last entry in the children array
25echo "--------- last\n";
26$last = $rootnode->lastChild;
27print_node($last);
28
29// The parent of this last node is the root again
30echo "--------- parent\n";
31$parent = $last->parentNode;
32print_node($parent);
33
34// The children of this parent are the same children as one above
35echo "--------- children of parent\n";
36$children = $parent->childNodes;
37print_node_list($children);
38
39echo "--------- creating a new attribute\n";
40//This is worthless
41//$attr = $dom->createAttribute("src", "picture.gif");
42//print_r($attr);
43
44//$rootnode->set_attributeNode($attr);
45$attr = $rootnode->setAttribute("src", "picture.gif");
46$attr = $rootnode->getAttribute("src");
47print_r($attr);
48print "\n";
49
50echo "--------- Get Attribute Node\n";
51$attr = $rootnode->getAttributeNode("src");
52print_node($attr);
53
54echo "--------- Remove Attribute Node\n";
55$attr = $rootnode->removeAttribute("src");
56print "Removed " . $attr . " attributes.\n";
57
58echo "--------- attributes of rootnode\n";
59$attrs = $rootnode->attributes;
60print_node_list($attrs);
61
62echo "--------- children of an attribute\n";
63$children = $attrs->item(0)->childNodes;
64print_node_list($children);
65
66echo "--------- Add child to root\n";
67$myelement = new domElement("Silly", "Symphony");
68$newchild = $rootnode->appendChild($myelement);
69print_node($newchild);
70print $dom->saveXML();
71print "\n";
72
73echo "--------- Find element by tagname\n";
74echo "    Using dom\n";
75$children = $dom->getElementsByTagname("Silly");
76print_node_list($children);
77
78echo "    Using elem\n";
79$children = $rootnode->getElementsByTagName("Silly");
80print_node_list($children);
81
82echo "--------- Unlink Node\n";
83print_node($children->item(0));
84$rootnode->removeChild($children->item(0));
85print_node_list($rootnode->childNodes);
86print $dom->savexml();
87
88echo "--------- Find element by id\n";
89print ("Not implemented\n");
90
91echo "--------- Check various node_name return values\n";
92print ("Not needed\n");
93
94?>
95