xref: /PHP-7.2/ext/simplexml/examples/interop.php (revision 4e5d4006)
1<?php
2$dom = new domDocument;
3$dom->load("book.xml");
4if(!$dom) {
5  echo "Error while parsing the document\n";
6  exit;
7}
8print "As SimpleXML\n";
9
10$s = simplexml_import_dom($dom);
11$books = $s->book;
12foreach ($books as $book) {
13	echo "{$book->title} was written by {$book->author}\n";
14}
15
16print "As DOM \n";
17
18$dom = dom_import_simplexml($s);
19$books = $dom->getElementsByTagName("book");
20foreach ($books as $book) {
21       $title = $book->getElementsByTagName("title");
22       $author = $book->getElementsByTagName("author");
23       echo $title[0]->firstChild->data . " was written by ". $author[0]->firstChild->data . "\n";
24}
25
26
27?>
28