Hi,

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.


books.xml

<?xml version="1.0" encoding="utf-8"?>
<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>
</perpustakaan>

exercise3_1.php

<?php

// 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 ?

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

exercise3_1.php

<?php

// 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:

<?php
$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 ?

exercise3_2.php

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

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, networking, learning, and sharing knowledge.