【发布时间】:2013-01-29 17:16:55
【问题描述】:
我想用 SimpleXML 解析 PHP 中的 XML 文档。
我使用了simplexml_load_file,我得到了这个错误:
Warning: simplexml_load_file(): [myfile] parser error : XML declaration allowed only at the start of the document in /www/[...]/myphpfile.php on line 7
我在 Google 上搜索过,问题似乎是 XML 文档中的标签前有一些空格。我查了一下,之前没有空格。
这是我的代码:
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;
}
编辑:错误是Failed loading XML XML declaration allowed only at the start of the document Extra content at the end of the document
XML 文档的第一个标签是<?xml version="1.0" encoding="UTF-8"?>
【问题讨论】:
-
您的 XML 文件来源在哪里?
-
两个子目录之后。