Back to index

php5  5.3.10
interop.php
Go to the documentation of this file.
00001 <?php
00002 $dom = new domDocument;
00003 $dom->load("book.xml");
00004 if(!$dom) {
00005   echo "Error while parsing the document\n";
00006   exit;
00007 }
00008 print "As SimpleXML\n";
00009 
00010 $s = simplexml_import_dom($dom);
00011 $books = $s->book;
00012 foreach ($books as $book) {
00013        echo "{$book->title} was written by {$book->author}\n";
00014 }
00015 
00016 print "As DOM \n";
00017 
00018 $dom = dom_import_simplexml($s);
00019 $books = $dom->getElementsByTagName("book");
00020 foreach ($books as $book) {
00021        $title = $book->getElementsByTagName("title");
00022        $author = $book->getElementsByTagName("author");
00023        echo $title[0]->firstChild->data . " was written by ". $author[0]->firstChild->data . "\n";
00024 }
00025 
00026 
00027 ?>