Php 警告:DOMDocument::load()[DOMDocument.load]:文档末尾的额外内容

Php 警告:DOMDocument::load()[DOMDocument.load]:文档末尾的额外内容,php,html,ajax,xml,dom,Php,Html,Ajax,Xml,Dom,嗨,我正在尝试将数据附加到现有文件中,如果不创建新文件并保存它的话。 相反,我的代码只保存一次数据。下一个数据集只覆盖第一个数据集。 请帮我找出我做错了什么 在注释之后,我更改了代码,现在我以 警告:DOMDocument::load()[DOMDocument.load]:中文档末尾的额外内容 function toXml($reg) { $xmlFile = "customers.xml"; $doc = new DomDocument('1

嗨,我正在尝试将数据附加到现有文件中,如果不创建新文件并保存它的话。 相反,我的代码只保存一次数据。下一个数据集只覆盖第一个数据集。 请帮我找出我做错了什么

在注释之后,我更改了代码,现在我以 警告:DOMDocument::load()[DOMDocument.load]:中文档末尾的额外内容

    function toXml($reg)
    {

        $xmlFile = "customers.xml";
        $doc = new DomDocument('1.0');
        $doc->load( $xmlFile);

        $cusCart = $doc -> createElement('cusCart');
        //$cusCart = $doc->appendChild($cusCart);

        foreach ($reg as $item => $val )

        {
            $customer = $doc->createElement('customer');
            $customer = $cusCart->appendChild($customer);

            $cusId = $doc->createElement('cusId');
            $cusId = $customer->appendChild($cusId);
            $value = $doc->createTextNode($item);
            $value = $cusId->appendChild($value);


            $fName = $doc->createElement('fName');
            $fName = $customer->appendChild($fName);
            $value1 = $doc->createTextNode($val["fName"]);
            $value1 = $fName->appendChild($value1);

            $lName = $doc->createElement('lName');
            $lName = $customer->appendChild($lName);
            $value2 = $doc->createTextNode($val["lName"]);
            $value2 = $lName->appendChild($value2);

            $phone = $doc->createElement('phone');
            $phone = $customer->appendChild($phone);
            $value3 = $doc->createTextNode($val["phone"]);
            $value3 = $phone->appendChild($value3);


            $email = $doc->createElement('email');
            $email = $customer->appendChild($email);
            $value4 = $doc->createTextNode($val["email"]);
            $value4 = $email->appendChild($value4);


            $pwd = $doc->createElement('pw');
            $pwd = $customer->appendChild($pwd);
            $value5 = $doc->createTextNode($val["pw"]);
            $value5 = $pwd->appendChild($value5);


        }

        $cusCart = $doc->appendChild($cusCart);     
        $doc->save($xmlFile);

        $strXml = $doc->saveXML();
        echo("<br>----- DATA RECOREDED!!! ----");

        return $strXml;
    }
函数toXml($reg)
{
$xmlFile=“customers.xml”;
$doc=新DomDocument('1.0');
$doc->load($xmlFile);
$cusCart=$doc->createElement('cusCart');
//$cusCart=$doc->appendChild($cusCart);
foreach($reg as$item=>$val)
{
$customer=$doc->createElement('customer');
$customer=$cusCart->appendChild($customer);
$cusId=$doc->createElement('cusId');
$cusId=$customer->appendChild($cusId);
$value=$doc->createTextNode($item);
$value=$cusId->appendChild($value);
$fName=$doc->createElement('fName');
$fName=$customer->appendChild($fName);
$value1=$doc->createTextNode($val[“fName”]);
$value1=$fName->appendChild($value1);
$lName=$doc->createElement('lName');
$lName=$customer->appendChild($lName);
$value2=$doc->createTextNode($val[“lName”]);
$value2=$lName->appendChild($value2);
$phone=$doc->createElement('phone');
$phone=$customer->appendChild($phone);
$value3=$doc->createTextNode($val[“phone”]);
$value3=$phone->appendChild($value3);
$email=$doc->createElement('email');
$email=$customer->appendChild($email);
$value4=$doc->createTextNode($val[“email”]);
$value4=$email->appendChild($value4);
$pwd=$doc->createElement('pw');
$pwd=$customer->appendChild($pwd);
$value5=$doc->createTextNode($val[“pw”]);
$value5=$pwd->appendChild($value5);
}
$cusCart=$doc->appendChild($cusCart);
$doc->save($xmlFile);
$strXml=$doc->saveXML();
回声(
----数据记录!!!----); 返回$strXml; }
在将节点附加到其父节点之前,应将子节点和属性添加到节点。请尝试以下方法

function toXml($reg){
    $doc = new DomDocument('1.0');
    $doc->load( 'cusXML.xml');

    $cusCart = $doc -> createElement('cusCart');
    foreach ($reg as $item => $val )
    {
        $customer = $doc->createElement('customer');

        // Create cusId node
        $cusId = $doc->createElement('cusId');
        $value = $doc->createTextNode($item);
        $value = $cusId->appendChild($value);
        $cusId = $customer->appendChild($cusId);

        // Create pw node
        $pwd = $doc->createElement('pw');
        $value5 = $doc->createTextNode($val["pw"]);
        $value5 = $pwd->appendChild($value5);
        $pwd = $customer->appendChild($pwd);

        $customer = $cusCart->appendChild($customer);
    }

    $cusCart = $doc->appendChild($cusCart);

    if(file_exists('cusXML.xml')) { $doc->save("cusXML.xml"); }
    else{ $doc->save("cusXML.xml"); }

    $strXml = $doc->saveXML();
    echo("<br>----- DATA RECOREDED!!! ----");
    return $strXml;
}
函数toXml($reg){
$doc=新DomDocument('1.0');
$doc->load('cusXML.xml');
$cusCart=$doc->createElement('cusCart');
foreach($reg as$item=>$val)
{
$customer=$doc->createElement('customer');
//创建cusId节点
$cusId=$doc->createElement('cusId');
$value=$doc->createTextNode($item);
$value=$cusId->appendChild($value);
$cusId=$customer->appendChild($cusId);
//创建pw节点
$pwd=$doc->createElement('pw');
$value5=$doc->createTextNode($val[“pw”]);
$value5=$pwd->appendChild($value5);
$pwd=$customer->appendChild($pwd);
$customer=$cusCart->appendChild($customer);
}
$cusCart=$doc->appendChild($cusCart);
如果(文件_存在('cusXML.xml'){$doc->save(“cusXML.xml”);}
else{$doc->save(“cusXML.xml”);}
$strXml=$doc->saveXML();
回声(
----数据记录!!!----); 返回$strXml; }
您可以在XML文档结尾后添加一个
文本
标记
。像

<?xml version="1.0"?>
<document>
    <article>sample</article>
</document>
</xml>

样品
所以只需删除
根元素之后的其他文本即可。在上面的示例中,根元素是
,其他文本或元素是


在动态生成XML时,请在进一步处理之前先检查XML,并在根元素结束后确定其他文本。

这是由于XML文件格式不正确造成的。 我的xml文件是

<cusCart> <customer> <cusid>...</cusid> </customer> </cusCart>
。。。
在我的代码中,我每次都调用(根元素)。所以每次我追加
发生


相反,我必须调用root一次来追加
谢谢你的提示

if/else(文件_exists('cusXML.xml')中的说明都是相同的。为什么要这样做?如果文件不存在,我正试图将数据保存到文件中……您的意思是“如果文件不存在,请执行此操作,否则请执行完全相同的操作”。在将XML加载到DOMDocument的代码行中,您需要更好的错误处理。首先修复该位置,处理加载失败的情况。您好,感谢您的代码,我做了一些更改,现在我以警告结束:DOMDocument::load()[DOMDocument.load]:文档末尾的额外内容转储XML字符串时会看到什么?