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.

user3942918
  • 25,539
  • 11
  • 55
  • 67
APonelle
  • 3
  • 4
  • 1
    you should put well formed xml to the simplexml_load_string(). http://stackoverflow.com/questions/143122/using-simplexml-to-create-an-xml-object-from-scratch – Phantom Jun 26 '14 at 12:38
  • 1
    since line 2 is an include file, make sure that there is no output (space, line break, bom element). – andrew Jun 26 '14 at 12:42
  • This page should leave no questions: http://www.php.net/manual/en/simplexml.examples-basic.php – ToBe Jun 26 '14 at 12:43
  • I have added the following code : `header("Content-Type: application/xml");` – APonelle Jun 26 '14 at 14:52

1 Answers1

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.

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • 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 Jun 26 '14 at 14:56
  • @APonelle Right click the page and do 'View Source' or open element inpector / firebug . And look in the Net Tab. – nl-x Jun 26 '14 at 14:59