summaryrefslogtreecommitdiff
path: root/ext/dom/examples
diff options
context:
space:
mode:
authorSterling Hughes <sterling@php.net>2003-06-05 17:51:35 +0000
committerSterling Hughes <sterling@php.net>2003-06-05 17:51:35 +0000
commit7dadb69c8881e6ac1825d5fff34a18b32b5773c4 (patch)
tree26c8bf788b2ebe6b0f585b108d3285b0bbf7e206 /ext/dom/examples
parentb59c7c730777b7c21ac27ce6917432c8e6b2023e (diff)
downloadphp-git-7dadb69c8881e6ac1825d5fff34a18b32b5773c4.tar.gz
add tests/ files as examples that you can run and play around with
Diffstat (limited to 'ext/dom/examples')
-rw-r--r--ext/dom/examples/dom1.inc43
-rw-r--r--ext/dom/examples/dom1.php94
2 files changed, 137 insertions, 0 deletions
diff --git a/ext/dom/examples/dom1.inc b/ext/dom/examples/dom1.inc
new file mode 100644
index 0000000000..96acddc669
--- /dev/null
+++ b/ext/dom/examples/dom1.inc
@@ -0,0 +1,43 @@
+<?PHP
+$xmlstr = "<?xml version='1.0' standalone='yes'?>
+<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
+[ <!ENTITY sp \"spanish\">
+]>
+<!-- lsfj -->
+<chapter language='en'><title language='en'>Title</title>
+<para language='ge'>
+&sp;
+<!-- comment -->
+<informaltable language='&sp;kkk'>
+<tgroup cols='3'>
+<tbody>
+<row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row>
+<row><entry>a2</entry><entry>c2</entry></row>
+<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
+</tbody>
+</tgroup>
+</informaltable>
+</para>
+</chapter> ";
+
+function print_node($node)
+{
+ print "Node Name: " . $node->nodeName;
+ print "\nNode Type: " . $node->nodeType;
+ $child_count = count($node->childNodes);
+ print "\nNum Children: " . $child_count;
+ if($child_count <= 1){
+ print "\nNode Content: " . $node->nodeValue;
+ }
+ print "\n\n";
+}
+
+function print_node_list($nodelist)
+{
+ foreach($nodelist as $node)
+ {
+ print_node($node);
+ }
+}
+
+?>
diff --git a/ext/dom/examples/dom1.php b/ext/dom/examples/dom1.php
new file mode 100644
index 0000000000..8654b19c2f
--- /dev/null
+++ b/ext/dom/examples/dom1.php
@@ -0,0 +1,94 @@
+<?php
+require_once("dom1.inc");
+
+echo "Test 1: accessing single nodes from php\n";
+$dom = new domDocument;
+$dom->loadxml($xmlstr);
+if(!$dom) {
+ echo "Error while parsing the document\n";
+ exit;
+}
+
+// children() of of document would result in a memleak
+//$children = $dom->children();
+//print_node_list($children);
+
+echo "--------- root\n";
+$rootnode = $dom->documentElement;
+print_node($rootnode);
+
+echo "--------- children of root\n";
+$children = $rootnode->childNodes;
+print_node_list($children);
+
+// The last node should be identical with the last entry in the children array
+echo "--------- last\n";
+$last = $rootnode->lastChild;
+print_node($last);
+
+// The parent of this last node is the root again
+echo "--------- parent\n";
+$parent = $last->parentNode;
+print_node($parent);
+
+// The children of this parent are the same children as one above
+echo "--------- children of parent\n";
+$children = $parent->childNodes;
+print_node_list($children);
+
+echo "--------- creating a new attribute\n";
+//This is worthless
+//$attr = $dom->createAttribute("src", "picture.gif");
+//print_r($attr);
+
+//$rootnode->set_attributeNode($attr);
+$attr = $rootnode->setAttribute("src", "picture.gif");
+$attr = $rootnode->getAttribute("src");
+print_r($attr);
+print "\n";
+
+echo "--------- Get Attribute Node\n";
+$attr = $rootnode->getAttributeNode("src");
+print_node($attr);
+
+echo "--------- Remove Attribute Node\n";
+$attr = $rootnode->removeAttribute("src");
+print "Removed " . $attr . " attributes.\n";
+
+echo "--------- attributes of rootnode\n";
+$attrs = $rootnode->attributes;
+print_node_list($attrs);
+
+echo "--------- children of an attribute\n";
+$children = current($attrs)->childNodes;
+print_node_list($children);
+
+echo "--------- Add child to root\n";
+$myelement = new domElement("Silly", "Symphony");
+$newchild = $rootnode->appendChild($myelement);
+print_node($newchild);
+print $dom->saveXML();
+print "\n";
+
+echo "--------- Find element by tagname\n";
+echo " Using dom\n";
+$children = $dom->getElementsByTagname("Silly");
+print_node_list($children);
+
+echo " Using elem\n";
+$children = $rootnode->getElementsByTagName("Silly");
+print_node_list($children);
+
+echo "--------- Unlink Node\n";
+print_node($children[0]);
+$rootnode->removeChild($children[0]);
+print_node_list($rootnode->childNodes);
+print $dom->savexml();
+
+echo "--------- Find element by id\n";
+print ("Not implemented\n");
+
+echo "--------- Check various node_name return values\n";
+print ("Not needed\n");
+
+?>