0

I'm a beginner with XML on PHP. My page returns me the following PHP error: error on line 2 at column 1: Extra content at the end of the document

But, at line 2, there only is a "require_once", followed by 3 others "require_once", the path to the required file is ok, and it ends with a ';'. The line above is the "

I have this sample of code, which I think is the source of this error (in this order)

$xml = simplexml_load_string("<result/>");
$entitlements = $xml->addChild("entitlements");
$entitlements->addChild("productId", $productId);

then, below :

$fulfillmentXML = new SimpleXMLElement($result);
//some stuff with $fullfillmentXML 
echo $xml->asXML();

I do not understand exactly how the XML works in PHP, but I thought there was a problem creating a SimpleXMLElement() after the simplexml_load_string() call.

4
  • 1
    you should put well formed xml to the simplexml_load_string(). stackoverflow.com/questions/143122/… Commented Jun 26, 2014 at 12:38
  • 1
    since line 2 is an include file, make sure that there is no output (space, line break, bom element). Commented Jun 26, 2014 at 12:42
  • This page should leave no questions: php.net/manual/en/simplexml.examples-basic.php Commented Jun 26, 2014 at 12:43
  • I have added the following code : header("Content-Type: application/xml"); Commented Jun 26, 2014 at 14:52

1 Answer 1

3

Your error doesn't look like a PHP error, but rather a browser error generated when it tried to parse the supplied XML contents. Therefore line 2 of the error does not refer to line 2 of your PHP file, but to line 2 of your generated output.

Check your output (source of your generated page), and see what line 2 is there. Make sure you only have 1 root element, and that nothing comes after it.

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

2 Comments

How can i see the output source ? there is no text (of any kind) after the error I show in the screen, and inspecting the element with the browser only display the code from the error.
@APonelle Right click the page and do 'View Source' or open element inpector / firebug . And look in the Net Tab.

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.