Back to index

php5  5.3.10
dom1.php
Go to the documentation of this file.
00001 <?php
00002 require_once("dom1.inc");
00003 
00004 echo "Test 1: accessing single nodes from php\n";
00005 $dom = new domDocument;
00006 $dom->loadxml($xmlstr);
00007 if(!$dom) {
00008   echo "Error while parsing the document\n";
00009   exit;
00010 }
00011 
00012 // children() of of document would result in a memleak
00013 //$children = $dom->children();
00014 //print_node_list($children);
00015 
00016 echo "--------- root\n";
00017 $rootnode = $dom->documentElement;
00018 print_node($rootnode);
00019 
00020 echo "--------- children of root\n";
00021 $children = $rootnode->childNodes;
00022 print_node_list($children);
00023 
00024 // The last node should be identical with the last entry in the children array
00025 echo "--------- last\n";
00026 $last = $rootnode->lastChild;
00027 print_node($last);
00028 
00029 // The parent of this last node is the root again
00030 echo "--------- parent\n";
00031 $parent = $last->parentNode;
00032 print_node($parent);
00033 
00034 // The children of this parent are the same children as one above
00035 echo "--------- children of parent\n";
00036 $children = $parent->childNodes;
00037 print_node_list($children);
00038 
00039 echo "--------- creating a new attribute\n";
00040 //This is worthless
00041 //$attr = $dom->createAttribute("src", "picture.gif");
00042 //print_r($attr);
00043 
00044 //$rootnode->set_attributeNode($attr); 
00045 $attr = $rootnode->setAttribute("src", "picture.gif");
00046 $attr = $rootnode->getAttribute("src");
00047 print_r($attr);
00048 print "\n";
00049 
00050 echo "--------- Get Attribute Node\n";
00051 $attr = $rootnode->getAttributeNode("src");
00052 print_node($attr);
00053 
00054 echo "--------- Remove Attribute Node\n";
00055 $attr = $rootnode->removeAttribute("src");
00056 print "Removed " . $attr . " attributes.\n";
00057 
00058 echo "--------- attributes of rootnode\n";
00059 $attrs = $rootnode->attributes;
00060 print_node_list($attrs);
00061 
00062 echo "--------- children of an attribute\n";
00063 $children = $attrs->item(0)->childNodes;
00064 print_node_list($children);
00065 
00066 echo "--------- Add child to root\n";
00067 $myelement = new domElement("Silly", "Symphony");
00068 $newchild = $rootnode->appendChild($myelement);
00069 print_node($newchild);
00070 print $dom->saveXML();
00071 print "\n";
00072 
00073 echo "--------- Find element by tagname\n";
00074 echo "    Using dom\n";
00075 $children = $dom->getElementsByTagname("Silly");
00076 print_node_list($children);
00077 
00078 echo "    Using elem\n";
00079 $children = $rootnode->getElementsByTagName("Silly");
00080 print_node_list($children);
00081 
00082 echo "--------- Unlink Node\n";
00083 print_node($children->item(0));
00084 $rootnode->removeChild($children->item(0));
00085 print_node_list($rootnode->childNodes);
00086 print $dom->savexml();
00087 
00088 echo "--------- Find element by id\n";
00089 print ("Not implemented\n");
00090 
00091 echo "--------- Check various node_name return values\n";
00092 print ("Not needed\n");
00093 
00094 ?>