I would like to write new books in books.xml then print them on the screen.
I already successfully able to write the new book to books.xml yet, unable to print them on the screen. Why is it ?
<?php
// Tambahkan buku baru dengan menggunakan DOM
$dom = new DomDocument();
$dom->load("books.xml");
$book = $dom->createElement("buku");
$book->setAttribute("isbn", "0973589825");
$title = $dom->createElement("judul");
$text = $dom->createTextNode("php|architect’s Guide to PHP Design
Patterns");
$title->appendChild($text);
$book->appendChild($title);
$author = $dom->createElement("pengarang", "Jason E. Sweat");
$book->appendChild($author);
$publisher = $dom->createElement("penerbit", "Marco Tabini &
Associates, Inc.");
$book->appendChild($publisher);
$dom->documentElement->appendChild($book);
echo 'Wrote: ' . $dom->save("books.xml") . ' bytes'; // Wrote: 72 bytes
// tampilkan di layar
//$file = file_get_contents('books.xml');
$file = simplexml_load_file('book.xml');
// $buku = new SimpleXMLElement($file);
// foreach ($buku as $buku){
# echo buku attribute
//echo $buku['isbn']."<br>";
# echo childs
echo $file->judul."<br>";
// }
?>
Warning: DOMDocument::load() [domdocument.load]: Input is not proper UTF-8, indicate encoding ! Bytes: 0x92 0x73 0x20 0x47 in file:///C:/xampp/htdocs/php_exercise/books.xml, line: 13 in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 8
Fatal error: Call to a member function appendChild() on a non-object in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 21
line 8: $dom->load("books.xml");
line 21: $dom->documentElement->appendChild($book);
What's wrong with it?
Thanks.