I get this error:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 23: parser error : Premature end of data in tag catalog line 2 in C:\xampp\htdocs\php_exercise\exercise3_1.php on line 7

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

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

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

<?php

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

}

?>

line 7: foreach ($buku as $buku){

Why the error appears?

Thanks.

To quote your error message:

'String could not be parsed as XML'

It appears that your books.xml file in invalid; that is has syntax errors in one of it's <catalog> tags.

P.S.
You shouldn't use the same variable name on both sides of the as clause of the foreach loop. The right hand side should be a new variable name.

You mean like this?

<?php

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

$buku = new SimpleXMLElement($file); 

foreach ($buku as $buku2){        

# echo buku attribute   

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

# echo childs   

echo $buku2 -> judul."<br>"; 

}

?>

I still receive similar error message.

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 23: parser error : Premature end of data in tag catalog line 2 in C:\xampp\htdocs\php_exercise\exercise3_1.php on line 7

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

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

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

You are still receiving that error message because you seem to have ignored everything in my previous post before the "P.S." part. The answer you are looking for is in there.