简体   繁体   English

PHP错误加载,编辑然后保存XML DOMDocument

[英]PHP Error loading, editing, then saving XML DOMDocument

I am getting the following output when running a simple script which loads a XML file, adds a node with a few child nodes to the document, then saves the XML file. 运行一个加载XML文件的简单脚本,向该文档添加一个带有几个子节点的节点,然后保存XML文件时,我得到以下输出。 Here is the link to this XML file being referenced: http://msgrapp.com/test/ajaxchat/messages.xml 这是被引用的XML文件的链接: http : //msgrapp.com/test/ajaxchat/messages.xml

Warning: DOMDocument::load(): Extra content at the end of the document in /home1/dstamp/public_html/messages.xml, line: 3 in /home1/dstamp/public_html//sendMessage.php on line 4 警告:DOMDocument :: load():/home1/dstamp/public_html/messages.xml中文档末尾的额外内容,第4行,/ home1 / dstamp / public_html // sendMessage.php,第3行

<?php
    $doc = new DOMDocument('1.0');
    $doc->load('messages.xml');

    $root = $doc->createElement('MESSAGE');
    $root = $doc->appendChild($root);

    $dateNode = $doc->createElement('DATE');
    $dateNode = $root->appendChild($dateNode);
    $dateText = $doc->createTextNode(date("F j Y g:i a"));
    $dateText = $dateNode->appendChild($dateText);

    $senderNode = $doc->createElement('SENDER');
    $senderNode = $root->appendChild($senderNode);
    $senderText = $doc->createTextNode($_GET['sender']);
    $senderText = $senderNode->appendChild($senderText);

    $messageNode = $doc->createElement('TEXT');
    $messageNode = $root->appendChild($messageNode);
    $messageText = $doc->createTextNode($_GET['message']);
    $messageText = $messageNode->appendChild($messageText);

    $doc->save('messages.xml');
    echo $doc->saveXML();
?>

The extra content error is caused by having two of the same node, in this case the MESSAGE node, as a root element. 额外的内容错误是由两个相同的节点(在本例中为MESSAGE节点)作为根元素而引起的。

You could add a new root element MESSAGES for example, and then add more MESSAGE elements within that 例如,您可以添加一个新的根元素MESSAGES,然后在其中添加更多MESSAGE元素

This will Help you : Alternately getting the error (Extra content at the end of the document ) 这将为您提供帮助: 交替出现错误(文档末尾的其他内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM