0

Hello I am having this codes:

<?php

// Add new book using DOM

$dom = new DomDocument();
$dom->load("books.xml");
$book = $dom->createElement("book");
$book->setAttribute("isbn", "0973589825");
$title = $dom->createElement("title");
$text = $dom->createTextNode("php|architect’s Guide to PHP Design
Patterns");
$title->appendChild($text);
$book->appendChild($title);
$author = $dom->createElement("author", "Jason E. Sweat");
$book->appendChild($author);
$publisher = $dom->createElement("publisher", "Marco Tabini &amp;
Associates, Inc.");
$book->appendChild($publisher);
$dom->documentElement->appendChild($book);

// show them on the screen

$file = file_get_contents('books.xml');
$buku = new SimpleXMLElement($file); 
foreach ($buku as $buku){        

# echo buku attribute   
//echo $buku['isbn']."<br>";         
# echo childs   

echo $buku->judul."<br>";    
?>

I wonder why the new title hasn't been added. What did I did wrong in adding the new title ?

Edited by ~s.o.s~: moved thread to PHP forum

3
Contributors
13
Replies
14
Views
5 Years
Discussion Span
Last Post by veedeoo
0

How to save my changes? Is my codes good enough to save the new book addition ?

I only see the previous books that are in books.xml not the new one.

0

Wrote: 555 bytes
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 13: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0x92 0x73 0x20 0x47 in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 27

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <book isbn="0973589825"><title>php|architect’s Guide to PHP Design&#13; in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 27

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 27

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\php_exercise\exercise3_2.php:27 Stack trace: #0 C:\xampp\htdocs\php_exercise\exercise3_2.php(27): SimpleXMLElement->__construct('<?xml version="...') #1 {main} thrown in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 27

Line 27: $buku = new SimpleXMLElement($file);

Why is it ?

0

What should I do then?

I try revising the code to:

// Show them on screen

$file = file_get_contents('books.xml');
// $buku = new SimpleXMLElement($file); 
// foreach ($buku as $buku){        

# echo buku attribute   
//echo $buku['isbn']."<br>";         
# echo childs   

echo $file->judul."<br>";    
// }

New errors appear:

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);

0

Another error:

Wrote: 559 bytes
Notice: Trying to get property of non-object in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 34

line 34: echo $file->judul."<br>";

This is my xml file after I execute the program:

<?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 &amp;
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');
// $buku = new SimpleXMLElement($file); 
// foreach ($buku as $buku){        

# echo buku attribute   
//echo $buku['isbn']."<br>";         
# echo childs   

echo $file->judul."<br>";    
// }
?>
0

Then, what should I change it to ?

Also, why this error appears:
line 34: echo $file->judul."<br>";

0

I switch part of the codes to:

    //$file = file_get_contents('books.xml');
    $file = simplexml_load_file('book.xml');

this error appears:

Wrote: 559 bytes
Warning: simplexml_load_file()  [function.simplexml-load-file]: I/O warning : failed to load external entity "book.xml" in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 27

Notice: Trying to get property of non-object in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 35

line 27: $file = simplexml_load_file('book.xml');
line 35: echo $file->judul."<br>";
0

Hi,

Try this,,, this is my test codes from the other threads, and I have to modify it similar to your xml . You can use the code as an example of what you are trying to achieved. There should not be any problem mocking it to your own.

Here we go... simple DOM object with write and then read..

copy and save as books.xml

<?xml version="1.0" standalone="yes"?>
<books>
  <book>
  <author>Jack Herrington</author>
  <title>PHP Hacks</title>
  <publisher>O'Reilly</publisher>
  </book>
  <book>
  <author>Jack Herrington</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
  </book>
  </books>

Copy and save as books.php

<?php
## filename books.php
$xml = "books.xml";
   $doc = new DomDocument;
   $doc->Load($xml);

    $books = $doc->getElementsByTagName('books')->item(0);
    ## we iterate  book block
    $new_BookEntry = $doc ->createElement('book');
    ## items going inside the book block
    $new_AuthorEntry = $doc->createElement('author');
    $new_TitleEntry = $doc ->createElement('title');
    $new_PubEntry = $doc->createElement('publisher');

    ## prepare items to be added in xml file

    $authorNode = $doc ->createTextNode ('New Book');
    $titleNode = $doc ->createTextNode ('New Title');
    $pubNode = $doc->createTextNode ('new publisher');
    ## add new entries 
    $new_AuthorEntry-> appendChild($authorNode);
    $new_TitleEntry-> appendChild($titleNode);
    $new_PubEntry->appendChild($pubNode);

    $new_BookEntry-> appendChild($new_AuthorEntry);
    $new_BookEntry-> appendChild($new_TitleEntry);
    $new_BookEntry->appendChild($new_PubEntry);
    $books -> appendChild($new_BookEntry);

if($doc->save($xml)) echo "Sucess";

## read updated xml file
  ## uncomment codes below if use in different page.
 //$doc = new DOMDocument();
  //$doc->load( $xml );

  $records = $doc->getElementsByTagName( "book" );
  foreach( $records as $record )
  {
  $author = $record->getElementsByTagName( "author" );
  $author = $author->item(0)->nodeValue;

  $publishers = $record->getElementsByTagName( "publisher" );
  $publisher = $publishers->item(0)->nodeValue;

  $titles = $record->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;

  echo "<p>Title: ".$title."<br/>Author: ".$author."<br/>Publisher: ".$publisher."<br/></p>";
  }

?>

That's should update the books.xml with the new info. and then read the entire xml file, then print on the screen.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.