使用simplexml_load_file()通过PHP加载XML文件-文档末尾的额外内容


loading XML file via PHP using simplexml_load_file() - Extra content at the end of the document

我是XML新手,有以下问题。我的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <employee>
        <Name>John</Name>
        <Surname>K</Surname>
        <ID>234</ID>
        <e-mail>something@k.com</e-mail>
    </employee>
</employees>
</xml>

我正在尝试通过php:加载XML文件

<?php
    $filename='../DBfiles/employeesDB.xml';
    $xml=simplexml_load_file($filename);
?>

我得到了以下信息:

Warning: simplexml_load_file(): ../DBfiles/employeesDB.xml:12: parser error : Extra content 
at the end of the document in C:'path_to_my_file'www'phpAndAjax'add.php on line 4
Warning: simplexml_load_file(): &lt;/xml&gt; in C:'path_to_my_file'www'phpAndAjax'add.php on 
line 4
Warning: simplexml_load_file(): ^ in C:'path_to_my_file'www'phpAndAjax'add.php on line 4

删除

</xml>

从文件末尾开始。没有打开标记<xml>,有一个不需要关闭的格式声明。

您不必关闭文件,只需删除xml关闭标记

<?xml version="1.0" encoding="UTF-8"?>
 <employees>
  <employee>
    <Name>John</Name>
    <Surname>K</Surname>
    <ID>234</ID>
    <e-mail>something@k.com</e-mail>
 </employee>
</employees>