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

时间:2015-01-13 05:14:56

标签: php html xml

运行加载XML文件的简单脚本时,我得到以下输出,向文档添加一个带有几个子节点的节点,然后保存XML文件。以下是引用此XML文件的链接:http://msgrapp.com/test/ajaxchat/messages.xml

  

警告:DOMDocument :: load():文档末尾的额外内容   在/home1/dstamp/public_html/messages.xml中,行:3英寸   第4行/home1/dstamp/public_html//sendMessage.php

<?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();
?>

1 个答案:

答案 0 :(得分:2)

额外内容错误是由两个相同的节点(在本例中为MESSAGE节点)作为根元素引起的。

例如,您可以添加新的根元素MESSAGES,然后在其中添加更多MESSAGE元素

这会帮助您:Alternately getting the error (Extra content at the end of the document )