XML Parsing Error: junk after document element

Since I had to go through about 20 pages to find the cause of this error, I figured I’d throw up one more with the cause that I found. Hopefully it’ll help somebody spend less time debugging the problem than I had to spend.

I’ve got a PHP script outputting XML that’s called from an Ajax script. When I was testing the PHP output I got the error XML Parsing Error: junk after document element after trying to add another section to the XML.

What turned out the be the cause is that in adding the section I wound up with two root elements, so in effect there weren’t any root elements. I wrapped the whole thing in another element and it worked perfectly, 20 minutes later.

Question, Comments...

Do you have more questions. Please either leave a comment below or join us in our new forum.

5 Responses to “XML Parsing Error: junk after document element”

  1. Hi there!

    Thanks so much for sharing this! Worked like a charm.

    Best regards
    Skashi

  2. Barbara J Jenson on August 4th, 2009 at 2:31 am

    Hi,
    I got the same error in firefox 3. my xml response looks like the following:
    1249370351232{“position”:[],”scheduled_from”:[],”scheduled_to”:[]}

    The xml validator says that the message is well-formed, but firefox complains. Can anybody please explain to me what the hell is wrong.
    It drives me crazy

  3. Barbara J Jenson on August 4th, 2009 at 3:09 am

    he all tags have been removed from my string.
    around the number 124.. is an id-tag, and around the {…} is an obj tag, and around both, is an answer tag.

  4. Thanks.
    For me too it was the first clicked google item and it gave me the answer for my problem in 2 minutes!

  5. Thanks very much!
    after seeing this post, i am able to find the fix very quickly.
    - Rajesh

Leave a Reply