I am trying to simple php program to read the following xml and with loop foreach wants the book title (judul) to be printed on the screen.


<?xml version="1.0" encoding="utf-8"?>
	<buku isbn="01232312312">
		<judul>PHP Programming</judul>
		<pengarang>Whoever he is</pengarang>
		<penerbit>KomTek Publishing</penerbit>
	<buku isbn="01534534">
		<judul>Simple Programming</judul>
		<pengarang>No one know</pengarang>
		<penerbit>KomTek Publishing</penerbit>



// Reading books XML with the DOM

  $doc = new DOMDocument();
  $doc->load( 'books.xml' );
  $books = $doc->getElementsByTagName( "book" );
  foreach( $books as $book )
  $authors = $book->getElementsByTagName( "author" );
  $author = $authors->item(0)->nodeValue;
  $publishers = $book->getElementsByTagName( "publisher" );
  $publisher = $publishers->item(0)->nodeValue;
  $titles = $book->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  echo "$title - $author - $publisher\n";


The above code does not show me anything on screen. How to do so ?

Recommended Answers

All 5 Replies

You write book but in your xml the correspondent is buku, so change all them to match your xml:

$books = $doc->getElementsByTagName( "buku" );

Also consider to use SimpleXML: http://php.net/manual/en/book.simplexml.php
bye :)



// Reading books XML with the DOM

  $doc = new DOMDocument();
  $doc->load( 'books.xml' );
  $buku = $doc->getElementsByTagName( "buku" );
  foreach( $buku as $buku )
  $pengarang = $buku->getElementsByTagName( "pengarang" );
  $pengarang = $pengarang->item(0)->nodeValue;
  $penerbit = $buku->getElementsByTagName( "penerbit" );
  $penerbit = $penerbit->item(0)->nodeValue;
  $judul = $buku->getElementsByTagName( "judul" );
  $judul = $judul->item(0)->nodeValue;
  echo "$judul"."<br>";


Now, it works! but I am using DOM. How to to use SimpleXML? I read the manual and still do not quite get it. This is what I have tried and there are some error on the construction.

$doc = new SimpleXMLElement('books.xml');

echo $doc->getName() . "\n";

foreach ($doc->children() as $child)
    echo $child->getName() . "\n";

With SimpleXML, as in the examples page ( http://www.php.net/manual/en/simplexml.examples-basic.php ) you can do:

$file = file_get_contents('books.xml');
$books = new SimpleXMLElement($file);

foreach($books as $book)
        # echo buku attribute
	echo $book['isbn'];

        # echo childs
	echo $book->judul;
	echo $book->pengarang;
	echo $book->penerbit;

bye :)

One more thing, this time I am trying to add new books in the xml using DOM and then print them out on the screen. How to do so ?


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

It does not show me anything and also does not save the book title.

Sorry for delay, I forgot to reply, just add $dom->save('books.xml'); at the end of your code and it will work, if you have write permission in the folder. Bye :)

p.s. just remember to replace book, title, author & publisher with buku, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.