0

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.

2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by Atli
1

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.

Edited by Atli: Added P.S.

-3

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

Edited by davy_yg

0

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.

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.