I want to parse an XML document in PHP with SimpleXML.
I used simplexml_load_file
and I got this error:
Warning: simplexml_load_file(): [myfile] parser error : XML declaration allowed only at the start of the document in /www/[...]/myphpfile.php on line 7
I have searched on Google and the problem seems to be that there is some whitespace before the tags in the XML document. I checked, there is no whitespace before.
Here is my code:
libxml_use_internal_errors(true);
$sxml=simplexml_load_file($xml);
if ($sxml) {
echo '<h2>'.$xml.'</h2>';
echo '<p>author: <textarea>';
foreach($sxml->author as $author) {
if($author!=$strXML)
echo $author.'\n';
}
echo '</textarea></p>';
}else {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error)
echo "\t", $error->message;
}
Edit: error is Failed loading XML XML declaration allowed only at the start of the document Extra content at the end of the document
The first tag of the XML document is <?xml version="1.0" encoding="UTF-8"?>