I have a file where I store comments. The file name is comments.xml:
<?xml version="1.0" encoding="utf-8"?>
<comment>
<user>User4251</user>
<date>02.10.2018</date>
<text>Comment body goes here</text>
</comment>
<comment>
<user>User8650</user>
<date>01.10.2018</date>
<text>Comment body goes here</text>
</comment>
To loop through the XML tree, I am using the example given at W3Schools (with some modifications to the parameters). The code is contained in index.php:
<?php
$xml = simplexml_load_file("comments.xml") or die("Error: Cannot create object");
foreach($xml -> children() as $comments) {
echo $comments -> user . ", ";
echo $comments -> date . ", ";
echo $comments -> text . "<br>";
}
?>
As per the example, I am expecting:
User4251, 02.10.2018, Comment body goes here
User8650, 02.10.2018, Comment body goes here
However, I am getting three errors:
Warning: simplexml_load_file(): comments.xml:7: parser error : Extra content at the end of the document in 192.168.0.1/users/User8650/index.php on line 2
Warning: simplexml_load_file(): in 192.168.0.1/users/User8650/index.php on line 2
Warning: simplexml_load_file(): ^ in 192.168.0.1/users/User8650/index.php on line 2
Error: Cannot create object
The fourth one is due to the die()
statement.
Is the example erroneous or am I going wrong somewhere?