We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,449 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

DOM

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.

3
Contributors
17
Replies
3 Days
Discussion Span
11 Months Ago
Last Updated
18
Views
davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2
niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

I add some more codes on the bottom to print them out on the screen.

<?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 = simplexml_load_file('book.xml');

// Loading the XML file
$file = simplexml_load_file("books.xml");
echo "<h2>".$file->getName()."</h2><br />";
foreach($file->children() as $book)
{
echo "BOOK : ".$book->attributes()->id."<br />";
echo "Isbn : ".$book->isbn." <br />";
echo "Pengarang : ".$book->author." <br />";
echo "Judul : ".$book->title." <br />";
echo "Penerbit : ".$book->genre." <br />";
echo "<hr/>";
}

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

I still receiving exactly the same error message. How to fix it?

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

I found several mistakes in your code.

'&' character

Repalce the & in the string literals to &amp;

Your language and English :)

You have created the XML file using some words comes in your language
Ex: "buku", "judul" etc.

And in the bottom you are trying to read using English words. So rewrite the code using those words

$file = simplexml_load_file("books.xml");
echo "<h2>".$file->getName()."</h2><br />";
foreach($file->children() as $book)
{
    echo "Author : ".$book->pengarang()."<br />";
    ...
}

Wrong Attribute

In your code as given below, you are refering to non existing attribute called 'id' and trying to get 'isbn' directly

$file = simplexml_load_file("books.xml");
echo "<h2>".$file->getName()."</h2><br />";
foreach($file->children() as $book)
{
    echo "BOOK : ".$book->attributes()->id."<br />";
    echo "Isbn : ".$book->isbn." <br />";
    ...
}

echo $file->title."<br>";

To access the ISBN values,

$file = simplexml_load_file("books.xml");
echo "<h2>".$file->getName()."</h2><br />";
foreach($file->children() as $book)
{
    echo "ISBN : ".$book->attributes()->isbn."<br />";
    ....
}

echo $file->title."<br>";
niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

One more thing. Remove the following line (line number 24)

$file = simplexml_load_file('book.xml');

book.xml does not exists.

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

You mean like this:

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

// tampilkan di layar
echo 'Wrote: ' . $dom->save("books.xml") . ' bytes'; // Wrote: 72 bytes

// Loading the XML file
$file = simplexml_load_file("books.xml");
echo "<h2>".$file->getName()."</h2><br />";
foreach($file->children() as $book)
{
echo "Isbn : ".$book->attributes()->isbn." <br />";
echo "Pengarang : ".$book->pengarang()." <br />";
echo "Judul : ".$book->judul()." <br />";
echo "Penerbit : ".$book->publisher()." <br />";
echo "<hr/>";
}

echo $file->title."<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);

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

Hmmmm..

Delete all the content in the XML file and put,

<?xml version="1.0"?>
<catalog>
</catalog>

Then try to run the script again

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

Which content are of xml file? Do you mean put all my codes in between <catalog></catalog> ?

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

I mean at the initial run, your XML file have to have following content. No need to change the PHP code

<?xml version="1.0"?>
<catalog>
</catalog>

I not sure, but let's give it a try :)

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

Which content are of xml file? Do you mean put all my codes in between <catalog></catalog> ?

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

No.

Now you have two files right?

  • books.xml
  • PHP file

At the begining books.xml file does not have any info about books right?
So put following into the books.xml and run the PHP file.

<?xml version="1.0"?>
<catalog>
</catalog>
niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

This is what I have in books.xml now:

<?xml version="1.0"?>
    <catalog>
    </catalog>

<perpustakaan>
    <buku isbn="01232312312">
        <judul>PHP Programming</judul>
        <pengarang>Whoever he is</pengarang>
        <penerbit>KomTek Publishing</penerbit>
    </buku>
    <buku isbn="01534534">
        <judul>Simple Programming</judul>
        <pengarang>No one know</pengarang>
        <penerbit>KomTek Publishing</penerbit>
    </buku>
<buku isbn="0973589825"><judul>php|architect�s Guide to PHP Design&#13;
Patterns</judul><pengarang>Jason E. Sweat</pengarang><penerbit>Marco Tabini &amp;&#13;
Associates, Inc.</penerbit></buku></perpustakaan>

and this error codes:

Warning: DOMDocument::load() [domdocument.load]: Extra content at the end of the document in file:///C:/xampp/htdocs/php_exercise/books.xml, line: 5 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

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

Create a PHP file from following script

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

    // tampilkan di layar
    echo 'Wrote: ' . $dom->save("books.xml") . ' bytes'; // Wrote: 72 bytes

    // Loading the XML file
    $file = simplexml_load_file("books.xml");
    echo "<h2>".$file->getName()."</h2><br />";
    foreach($file->children() as $book)
    {
        echo "Isbn : ".$book->attributes()->isbn." <br />";
        echo "Pengarang : ".$book->pengarang." <br />";
        echo "Judul : ".$book->judul." <br />";
        echo "Penerbit : ".$book->penerbit." <br />";
        echo "<hr/>";
    }
    echo $file->title."<br>";
?>

NOTE : Make sure & is still replaced by &amp; after you copy paste the code

Create books.xml file from following

<?xml version="1.0"?>
<catalog>
</catalog>

Now try to run the PHP file

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

ok, I'll try. I get this error message:

Warning: DOMDocument::save() [domdocument.save]: string is not in UTF-8 in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 24
Wrote: 717 bytes
catalog

Isbn :

Fatal error: Call to undefined method SimpleXMLElement::pengarang() in C:\xampp\htdocs\php_exercise\exercise3_2.php on line 32

line 24: $file = simplexml_load_file("books.xml");

line 32: echo "<hr/>";

Why is it ?

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

Try using $book->pengarang
NOT $book->pengarang(). Remove the parentheses

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

I already did. I get error on line 24 & 32.

line 24: $file = simplexml_load_file("books.xml");

line 32: echo "<hr/>";

I don't know what's wrong.

davy_yg
Master Poster
735 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

Can you post your edited,latest code?

niranga
Junior Poster
192 posts since Apr 2010
Reputation Points: 21
Solved Threads: 26
Skill Endorsements: 0

Hi,

I must have posted answers to this type or related question in the past. Here we go again take Four.. I want to do this my way, or we can just take the highway..

First, we create our new books.xml file

<?xml version="1.0" standalone="yes"?>
<books>
  <book>
  <author>Poor Boy or veedeoo</author>
  <title>Hacking is Bad</title>
  <publisher>the black book</publisher>
  </book>
  <book>
  <author>veedeoo</author>
  <title>The Secret of Hacking for Revenge</title>
  <publisher>the black book</publisher>
  </book>
</books>  

Second, we create the php script responsible for writing and displaying the changes made on the books.xml file. Let's call this as book.php .

<?php
    ## filename : book.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');

        $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

 //$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 do it..direct your browser to book.php and you should be able to read the newly added items.

Room for imporvements, the script above, should be so easy to modify for any purpose e.g. form based application.., or even database application.

Looking at the script above, it is also pretty easy to make a simple function out of it, or even a simple class to make a multiple entries on the xml file, without actually editing the book.php .

veedeoo
Master Poster
764 posts since Oct 2011
Reputation Points: 298
Solved Threads: 132
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1142 seconds using 2.76MB