]> granicus.if.org Git - php/commitdiff
- added interop between DOM and SimpleXML example
authorChristian Stocker <chregu@php.net>
Sun, 26 Oct 2003 19:15:52 +0000 (19:15 +0000)
committerChristian Stocker <chregu@php.net>
Sun, 26 Oct 2003 19:15:52 +0000 (19:15 +0000)
- added xpath example

ext/simplexml/examples/interop.php [new file with mode: 0644]
ext/simplexml/examples/xpath.php [new file with mode: 0644]

diff --git a/ext/simplexml/examples/interop.php b/ext/simplexml/examples/interop.php
new file mode 100644 (file)
index 0000000..9e38ec1
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+$dom = new domDocument;
+$dom->load("book.xml");
+if(!$dom) {
+  echo "Error while parsing the document\n";
+  exit;
+}
+print "As SimpleXML\n";
+
+$s = simplexml_import_dom($dom);
+$books = $s->book;
+foreach ($books as $book) {
+       echo "{$book->title} was written by {$book->author}\n";
+}
+
+print "As DOM \n";
+
+$dom = dom_import_simplexml($s);
+$books = $dom->getElementsByTagName("book");
+foreach ($books as $book) {
+       $title = $book->getElementsByTagName("title");
+       $author = $book->getElementsByTagName("author");
+       echo $title[0]->firstChild->data . " was written by ". $author[0]->firstChild->data . "\n";
+}
+
+
+?>
diff --git a/ext/simplexml/examples/xpath.php b/ext/simplexml/examples/xpath.php
new file mode 100644 (file)
index 0000000..23253d7
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+$books = simplexml_load_file('book.xml');
+
+$xpath_result = $books->xsearch("/books/book/title");
+foreach($xpath_result as $entry ) {
+    print "$entry \n";
+}
+
+?>