1

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. Here is the link to this XML file being referenced: 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

<?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
  • Please show us exactly what your XML file looks like. If the file is large then the last 10 lines would probably be enough. Edit your question and put the XML there; do not put it in a comment. Commented Jan 13, 2015 at 5:18

1 Answer 1

2

The extra content error is caused by having two of the same node, in this case the MESSAGE node, as a root element.

You could add a new root element MESSAGES for example, and then add more MESSAGE elements within that

This will Help you : Alternately getting the error (Extra content at the end of the document )

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks...one question though, when I load messages.xml into the $doc variable, what's the best way to access the root node? Is there an array of nodes that are accessible for DOMDocument objects? Or would I use $doc->getElementsByTagname("MESSAGES"). I need to be able to access the root node MESSAGES to append MESSAGE nodes to it. Also I noticed the answer in the link you posted is using the SimpleXML extension, I would like to achieve this without using it.
Nevermind, found the answer. Can access the root node to append children using $doc->documentElement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.