【问题标题】:Warning: DOMDocument::load() [domdocument.load]: Extra content at the end of the document警告:DOMDocument::load() [domdocument.load]:文档末尾的额外内容
【发布时间】:2013-10-29 09:00:24
【问题描述】:

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

在 cmets 之后,我更改了代码,现在我最终得到了 警告: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;
    }

【问题讨论】:

  • 你的 if/else (file_exists('cusXML.xml')) 中的指令都是一样的。为什么要这样做?
  • 如果文件不存在,我正在尝试将数据保存到文件中...
  • 您的意思是“如果文件不存在,请执行此操作,否则执行完全相同的操作”
  • 您需要对将 XML 加载到 DOMDocument 的代码行进行更好的错误处理。先修复那个位置,处理加载失败的情况。

标签: php html ajax xml dom


【解决方案1】:

子节点和属性应先添加到节点,然后再将其附加到其父节点。请尝试以下方法

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;
}

【讨论】:

  • 您好,感谢您的代码,我做了一些更改,现在我得到了警告:DOMDocument::load() [domdocument.load]: Extra content at the end
  • 如果转储 XML 字符串,您会看到什么?
【解决方案2】:

您可能在 XML 文档的末尾有一个额外的 texttag。喜欢

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

所以只需删除root 元素之后的其他文本。在上面的例子中,根元素是&lt;/document&gt;,其他文本或元素是&lt;/xml&gt;

由于您正在动态生成 XML,因此在进一步处理之前先检查您的 XML,并在根元素结束后识别其他文本。

【讨论】:

    【解决方案3】:

    这是由于 xml 文件格式错误造成的。 我的 xml 文件是

    <cusCart> <customer> <cusid>...</cusid> </customer> </cusCart>
    

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

    相反,我必须调用 root 一次来附加 &lt;customer&gt; &lt;cusid&gt;...&lt;/cusid&gt; &lt;/customer&gt;&lt;customer&gt; &lt;cusid&gt;...&lt;/cusid&gt; &lt;/customer&gt; 无论如何感谢您的提示!

    【讨论】:

      猜你喜欢
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      相关资源
      最近更新 更多